JAVA面向對象(8)
文件:存儲相關記錄或放在一起的數據的集合。
導入文件需要導入一個包: java.io.*;
File創建對象時需要關聯一個字符串:
File f=new File("[放入文件的路徑關聯指定的文件名或目錄名]")
顯示目錄下所以的文件或目錄:File [] files=對象名.listFiles();<輸出既包括文件有包括文件夾>
可通過 isFile方法判斷是否為文件
1kb=1024b(字節) 1MB=1024kb
public static void main(String [] args ){ File fileFile讀取文件操作=new File("D:\\Test\\hello.txt"); if(file.exists()){ //判斷是否有文件或文件夾存在 if(file.isFile()){ //如果是文件 System.out.println("名稱:"+file.getName()); System.out.println("相對路徑:"+file.getPath()); System.out.println("絕對路徑:"+file.getAbsolutePath()); System.out.println("文件大小為:"+file.length()); } if(file.isDirectory()){ System.out.println("此文件為目錄或文件夾!"); }else{ System.out.println("文件不存在!"); } } }
執行刪除操作時刪除文件夾時,文件夾裏面有子文件時不能刪除。
遞歸算法
import java.io.*;
import java.util.*;
public class digui {
public static void main(String [] args){
// File file=new File("D:\\Test");
// showInfo(file);
//非菠蘿契數列:1 1 2 3 5 8 ......
//int [] muns=new int[10]; //定義數組
Scanner input=new Scanner(System.in);
System.out.println("請輸入您要計算的位置:");
int a=input.nextInt();
System.out.println(suan(a-1));
}
private static void showInfo(File f){
File [] file=f.listFiles();
for(int i=0;i<file.length;i++){
System.out.println(file[i].getName()); //得到一級目錄中的文件和文件夾
if(file[i].isDirectory()){ //判斷一級目錄中是否含有文件夾
showInfo(file[i]);//調用自己方法
}
}
}
private static int suan(int i){
if(i==0 || i==1){
return i=1;
}else{
return suan(i-2)+suan(i-1);
}
}
}
Java中的流:
1.字節流: InputStream(輸入流:讀取文件)
InputStream input=new FileInputStream(對象名或文件路徑) <不能直接new>【不能顯示中文】
OutputStream(輸出流:寫入文件)
OutputStream output=new FileOutPutStream(對象名或文件路徑) <不能直接new>【寫入文件會覆蓋原文件或者會新建新文件】
public static void main(String [] args) throws Exception{ // //聲明流對象 // FileInputStream fis=null; // try { // fis =new FileInputStream("D:\\Test\\hello.txt"); // int data; // System.out.println("文件內容為:"); // // //循環讀取數據 // // while((data=fis.read())!=-1){ // System.out.print((char)data+""); // } // }catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // // } finally { // if(fis!=null){ // try { // fis.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // }FileInputStream
public static void main(String [] args){ OutputStream fos=null; try { String str="好好學習java"; String a="每天"+10+"分鐘"; byte [] words= str.getBytes(); //字節數組 //創建流的對象,以追加方式寫入文件 fos=new FileOutputStream("D:\\Test\\test.txt"); //寫入文件 try { fos.write(words); fos.write(a.getBytes()); System.out.println("Test文件已更新!"); } catch (IOException e) { System.out.println("文件創建時失敗!"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }OutputStream
2.字符流:Rearder(字符輸入流:讀取文件)
Rearder fr=new FileRearer(對象名或文件路徑) 【可以顯示中文】
BufferedReader字符輸入流:讀取文件)
BufferedReader br=new BufferedReader(只能放Rearder的對象名);
BufferedReader的效率比Rearder高些。
Writer(字符輸出流:寫入文件)
Writer fr=new File Writer(對象名或文件路徑)
public static void main(String [] args){ Reader fr=null; StringBuffer sbf=null; try { fr=new FileReader("D:\\Test\\test.txt"); sbf=new StringBuffer(); int length; while((length=fr.read())!=-1){ char c=(char)length; sbf.append(c); } System.out.println(sbf.toString()); //輸出數據 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fr!=null){ try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }Reader
public static void main(String [] args){ Reader fr=null; BufferedReader br=null; try { //先創建一個FileReader對象 fr=new FileReader("D:\\Test\\test.txt"); //在創建一個BufferedReader對象 br=new BufferedReader(fr); //讀取每一行數據 String line=br.readLine(); while(line!=null){ System.out.println(line); line=br.readLine();//再次賦值進行判斷 } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); //先關 fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }BufferedReader
public static void main(String [] args){ Writer fw=null; try { //創建一個FileWrite對象 fw=new FileWriter("D:\\Test\\簡介.txt"); //寫入信息 fw.write("我熱愛我的團隊!"+"\r\n"); fw.write("張三"+"\r\n"); //\r\n進行換行 fw.write("18歲"); fw.flush(); //刷新緩沖區 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Writer
3.DataInputStream字節流來輸出二進制的文件 是filterInputStream的子類
DataOutPutStream字節流來寫入二進制的文件 是filterOutputStream的子類
二進制文件(亂碼)能夠增強文件的安全性
實體類 可實現一個序列化操作的接口 做數據傳遞的載體 二進制(0和1)、XML形式(標記《可跨平臺跨語言》)
JAVA面向對象(8)