標籤:
第一種分法:1.輸入資料流2.輸出資料流第二種分法:1.位元組流2.字元流第三種分法:1.節點流2.處理流////////////////////////////////////////////////////////////////////////////位元組流:InputStream 的子類是? FileInputStream;InputStream :int read(byte[]b,int off,int len)從第幾位讀,讀幾個,返回讀取的個數OutputStream 的子類是?FileOutputStreamOutputStream :void write(byte[]b,int off,int len)public static void main(String args[]){FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("e:/from.txt");//輸入資料流(從檔案讀資料,即資料輸入到程式裡) out = new FileOutputStream("e:/to.txt");//輸出資料流(寫入檔案資料,即資料輸出到檔案裡) byte[] buffer =new byte[100];//位元組數組,用來存放讀取的資料 int num = in.read(buffer, 0, buffer.length);//從第0位讀取,讀from.txt檔案的 buffer.length個,放進buffer內 out.write(buffer, 0, num);//read方法返回讀取的個數 //out.write(buffer, 0, 2);//從0位開始,把buffer數組的2個位元組寫進to.txt檔案裡 for(int i = 0;i<buffer.length;i++){ System.out.print(buffer[i]); }String s = new String(buffer);//轉換成原來的字元s= s.trim();//trim方法:去掉首尾空格和Null 字元,中間的空格不去掉 System.out.print(s); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}讀取大檔案:public static void main(String args[]) { FileInputStream in = null; FileOutputStream out = null; int a = 0; try { in = new FileInputStream("e:/from.txt");//輸入資料流(從檔案讀資料,即資料輸入到程式裡) out = new FileOutputStream("e:/to.txt");//輸出資料流(寫入檔案資料,即資料輸出到檔案裡) byte[] buffer =new byte[1024];//位元組數組,用來存放讀取的資料 while(true){ a++; System.out.print(a); int num = in.read(buffer, 0, buffer.length);//從第0位讀取,讀from.txt檔案的 buffer.length個,放進buffer內 out.write(buffer, 0, num);//read方法返回讀取的個數 if(num == -1){//返回-1,說明讀取完畢 break; } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }////////////////////////////////////////字元流:讀寫檔案時,以字元為基礎位元組輸入資料流:Reader 的子類是?FileReaderint read(char [] c,int off,int len)位元組輸出資料流:Writer 的子類是?FileWritervoid write(char [] c,int off,int len)public static void main(String args[]) { FileReader reader =null; FileWriter writer = null; try{ reader = new FileReader("E:/from.txt"); writer = new FileWriter("e:/to.txt"); char [] buffer = new char[100]; int num = reader.read(buffer, 0, buffer.length); writer.write(buffer, 0, num); for(int i = 0;i<num;i++){ System.out.print(buffer[i]); } } catch(Exception e){ System.out.print(e); } finally{ try{ reader.close(); writer.close(); } catch(Exception e){ System.out.print("success!!!"); } }}/////////////////////////////////////////////////一次性讀取一行資料public static void main(String args[]) { FileReader fileReader = null; BufferedReader bufferedReader = null; try{ fileReader = new FileReader("e:/from.txt"); bufferedReader = new BufferedReader(fileReader); String line = null; while(true){ line = bufferedReader.readLine(); if(line == null){ break; } System.out.println(line); } } catch(Exception e){ System.out.println(e); } finally{ try{ bufferedReader.close(); fileReader.close(); } catch(Exception e){ System.out.println(e); } }
IO流基本操作