1.位元組流: FileInputStream(位元組輸入流) 特有方法: FileInputStream fis = new FileInputStream("temp.txt"); 1.int num = fis.available();//返迴文件的位元組總個數 2.fis.read()//返回in ...
1.位元組流:
FileInputStream(位元組輸入流)
特有方法:
FileInputStream fis = new FileInputStream("temp.txt");
1.int num = fis.available();//返迴文件的位元組總個數
2.fis.read()//返回int型數字,如果沒有讀到位元組,返回-1
1 //使用byte數組進行優化 2 public static void m3()throws IOException 3 { 4 5 FileInputStream fis = new FileInputStream("temp.txt"); 6 7 byte[] arr=new byte[10]; 8 int len=0; 9 while((len=fis.read(arr))!=-1){ 10 11 System.out.print(new String(arr,0,len)); 12 } 13 14 fis.close(); 15 16 17 }
1 //使用位元組流讀取文件:一次對一個位元組 2 public static void m2()throws IOException 3 { 4 FileInputStream fis = new FileInputStream("temp.txt"); 5 6 int num; 7 while((num=fis.read())!=-1){ 8 9 System.out.print((char)num); 10 } 11 12 fis.close(); 13 14 }
2.FileOutputStream(位元組輸出流)
方法:
FileOutputStream fos = new FileOutputStream("temp.txt");
fos.write("abc".getBytes());//byte[] getBytes() 編碼
註意:1.在輸出時,要在每一次輸入結束後都要進行flush(),如果不刷新,則不會輸出
2.在程式結尾都要close(),關閉文件,並且close()預設自動刷新
3.位元組緩衝流:BufferedInputStream BufferedOutputStream
這幾個方法都會拋出異常。
1 例: 2 import java.io.*; 3 class Demo4 4 { 5 public static void main(String[] args) 6 { 7 //位元組緩衝流:BufferedInputStream BufferedOutputStream 8 9 //複製圖片 10 BufferedInputStream bis = null; 11 BufferedOutputStream bos = null; 12 13 try{ 14 15 bis = new BufferedInputStream(new FileInputStream("tou1.jpg")); 16 bos = new BufferedOutputStream(new FileOutputStream("tou1_copy.jpg")); 17 18 byte[] arr=new byte[1024]; 19 int len=0; 20 while((len=bis.read(arr))!=-1){ 21 22 bos.write(arr,0,len); 23 } 24 25 }catch(IOException e){ 26 e.printStackTrace(); 27 }finally{ 28 29 if(bis!=null) 30 try{ 31 bis.close(); 32 }catch(IOException e){ 33 throw new RuntimeException("位元組緩衝讀取流關閉失敗"); 34 } 35 36 if(bos!=null) 37 try{ 38 bos.close(); 39 }catch(IOException e){ 40 throw new RuntimeException("位元組緩衝輸出流關閉失敗"); 41 } 42 } 43 44 } 45 }
4.標準輸入(和鍵盤關聯):System.in(in是一個位元組輸入流對象(預設關聯的設備是鍵盤))
//int m = kk.read();//從鍵盤讀數據 阻塞式方法,只要不輸入,會一直等待
//System.out.print((char)m);
標準輸出(已經和控制台設備關聯)System.out;(out是一個位元組輸出流對象(預設關聯的設備是控制台))//是一個位元組輸出流對象
使用System.in實現鍵盤輸入,太麻煩
System.in實現鍵盤輸入的代碼類似於BufferedReader的一次讀一行功能的代碼(readLine())
System.in ----位元組讀取流
BufferedReader---- 字元讀取流
5.轉換流:
InputStreamReader:是從位元組流到字元流的橋
InputStreamReader(InputStream in)
OutputStreamWriter :OutputStreamWriter(OutputStream out)
1 例: 2 class Demo6 3 { 4 public static void main(String[] args) throws IOException 5 { 6 //定義標準的鍵盤位元組輸入流對象,或者定義FileInputStream()對象 7 InputStream in=System.in; 8 9 //使用轉換流把位元組流轉成字元流 10 InputStreamReader isr = new InputStreamReader(in); 11 12 //使用字元緩衝流 13 BufferedReader br = new BufferedReader(isr); 14 15 16 17 //定義標準的位元組輸出流對象,或者定義FileOutputStream()對象 18 PrintStream out = System.out;//是一個位元組輸出流對象(已經和控制台設備關聯了) 19 20 //把位元組輸出流轉成字元輸出流 21 OutputStreamWriter osw = new OutputStreamWriter(out); 22 23 //使用字元緩衝流 24 BufferedWriter bw = new BufferedWriter(osw); 25 26 27 String line = null; //從鍵盤讀不到結束標誌 28 while((line=br.readLine())!=null){ 29 if("over".equals(line)) 30 break; 31 bw.write(line); 32 bw.newLine(); 33 // bw.flush(); 34 //System.out.println(line);//位元組流 35 } 36 37 38 br.close(); 39 bw.close(); 40 41 } 42 }
6.File:java把路徑或文件面向對象
實例化對象:
//1.把文件或文件夾封裝成File類型的對象
File file=new File("E:\\java練習\\day17練習\\tt.txt");
//2.適合文件名變化的
File file=new File("E:\\java練習\\day17練習","tt.txt");
//3.分開定義
File ff=new File("E:\\java練習\\day17練習");
File file=new File(ff,"tt.txt");
方法:
getName()//得到文件名
getPath()//得到路徑,定義的是相對的則得到相對的,定義的絕對的獲取絕對的
getParent()//返回此抽象路徑名父目錄的路徑名字元串;如果此路徑名沒有指定父目錄,則返回 null。
getAbsolutePath()//得到絕對路徑
lastModified()//獲取文件最後的修改時間
delete()//刪除文件或文件夾
listRoots()//得到根目錄(得到系統盤)
list()//得到某個目錄下的所有文件和文件夾的名字返回的是String[]
listFile()//返回File[] 把某個目錄下的所有文件和文件夾都封裝成File的對象
listFile(FilenameFilter filter)
7.遞歸:函數自己調用自己,當調用次數太多時,會記憶體溢出,所有要註意,調用次數不要太多
註意:要有調用的結束條件
8.PrintStream:位元組列印流,繼承了OutputStream,位元組輸出流的功能都具備,增加了列印的功能
向哪些設備輸出(目的):
1: File類型的文件
2:OutputStream類型的位元組輸出流
3:字元串類型的文件名
例:ps.write(353);// 00000000 00000000 00000001 01100001---》砍掉前三個位元組 01100001
9.父類是Writer
PrintWriter:字元列印流,字元輸出流的基本功能都具備,增加了列印功能
向哪些設備輸出(目的):
1:File類型的文件
2:位元組輸出流對象
3:字元輸出流對象
4:字元串類型的文件名
1 例: 2 3 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 4 5 // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 6 7 PrintWriter pw = new PrintWriter(System.out,true);//加上第二個參數,則會自動刷新 8 9 String line = null; 10 while((line=br.readLine())!=null){ 11 if("over".equals(line)) 12 break; 13 pw.println(line);//會輸出換行 14 //pw.write(line); 15 // pw.flush(); 16 }