java 對於文件的操做主要分爲兩大類java
字符流windows
字節流數組
其中,字符流有兩個抽象類:Writer Reader函數
其對應子類 FileWriter 和 FileReader 可實現文件的讀寫操做優化
BufferedWriter 和BufferedReader可以提供緩衝區功能,用以提升效率指針
一樣,字節流也有兩個抽象類:InputStream OutputStreamcode
其對應子類有FileInputStream和FileOutputStream實現文件讀寫對象
BufferedInputStream 和BufferedOutputStream提供緩衝區功能內存
package wm_io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.security.cert.TrustAnchor; public class test { public static void main(String[] args) { // TODO Auto-generated method stub //fun1(); //fun2(); //fun3(); //fun4(); //fun5(); //fun6(); //fun7(); //fun8(); fun9(); } /** * 字節流,向文件中寫入字符串 */ public static void fun1(){ String FileName="D:"+File.separator+"test.txt"; FileOutputStream o=null; try { o=new FileOutputStream(FileName); String string="字節流,向文件中寫入字符串\r\n"; byte[] b=string.getBytes(); o.write(b); o.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ if(o!=null){ try { o.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * 字節流, 向文件中一個字節一個字節的寫入字符串 */ public static void fun2(){ String FileName="D:"+File.separator+"test.txt"; FileOutputStream o=null; try { o=new FileOutputStream(FileName); String string="字節流, 向文件中一個字節一個字節的寫入字符串\r\n"; byte[] b=string.getBytes(); for(int i=0;i<b.length;i++){ o.write(b[i]); } o.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ if(o!=null){ try { o.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * 字節流,向文件中追加新內容 */ public static void fun3(){ String FileName="D:"+File.separator+"test.txt"; FileOutputStream o=null; try { o=new FileOutputStream(FileName,true); String string="向文件中追加新內容\r\n"; byte[] b=string.getBytes(); o.write(b); } catch (Exception e) { e.printStackTrace(); }finally{ if(o!=null){ try { o.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * 字節流,讀取文件 */ public static void fun4(){ String FileName="D:"+File.separator+"test.txt"; FileInputStream i =null; try { i=new FileInputStream(FileName); //方式二:數組循環讀取 //當讀取到文件末尾時會返回-1 byte[] b=new byte [1024]; int len=0; while((len = i.read(b)) != -1) { System.out.println(new String(b,0,len)); } /*//方式三:標準大小的數組讀取 //定一個一個恰好大小的數組 //available()方法返回文件的字節數 //若是文件過大,內存會溢出 byte[] buf = new byte[i.available()]; i.read(buf); //由於數組大小恰好,因此轉換爲字符串時無需在構造函數中設置起始點 System.out.println(new String(buf)); */ } catch (Exception e) { e.printStackTrace(); }finally{ if(i!=null){ try { i.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * 字節流,二進制文件的複製 */ public static void fun5(){ String bin="D:"+File.separator+"test.txt"; String copy="D:"+File.separator+"test2.txt"; FileInputStream i=null; FileOutputStream o =null; try { i = new FileInputStream(bin); o = new FileOutputStream(copy); //循環的方式讀入寫出文件,從而完成複製 byte[] buf = new byte[1024]; int temp = 0; while((temp = i.read(buf)) != -1) { o.write(buf, 0, temp); } } catch (Exception e) { e.printStackTrace(); }finally { if(i != null) { try { i.close(); } catch (IOException e) { e.printStackTrace(); } } if(o != null) { try { o.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 字符流,寫入 */ public static void fun6(){ String FileName="D:"+File.separator+"test.txt"; FileWriter w=null; try { w=new FileWriter(FileName); w.write("字符流,寫入\r\n"); } catch (Exception e) { e.printStackTrace(); }finally{ if(w!=null){ try { w.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * 字符流,讀出 */ public static void fun7(){ String FileName="D:"+File.separator+"test.txt"; FileReader r=null; try { r=new FileReader(FileName); //方式三:循環讀取的簡化操做 //單個字符讀取,當temp不等於-1的時候打印字符 /*int temp = 0; while ((temp = r.read()) != -1) { System.out.print((char)temp); } */ //方式四:讀入到字符數組 /* char[] buf = new char[1024]; int temp = r.read(buf); //將數組轉化爲字符串打印,後面參數的意思是 //若是字符數組未滿,轉化成字符串打印後尾部也許會出現其餘字符 //所以,讀取的字符有多少個,就轉化多少爲字符串 System.out.println(new String(buf,0,temp)); */ //方式五:讀入到字符數組的優化 //因爲有時候文件太大,沒法肯定須要定義的數組大小 //所以通常定義數組長度爲1024,採用循環的方式讀入 char[] buf = new char[1024]; int temp = 0; while((temp = r.read(buf)) != -1) { System.out.print(new String(buf,0,temp)); } } catch (Exception e) { e.printStackTrace(); }finally{ if(r != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 字符流,文本文件的複製 */ public static void fun8(){ String bin="D:"+File.separator+"test.txt"; String copy="D:"+File.separator+"test2.txt"; FileReader r = null; FileWriter w =null; try { r=new FileReader(bin); w=new FileWriter(copy); //方式一:單個字符寫入 int temp = 0; while((temp = r.read()) != -1) { w.write(temp); } //方式二:字符數組方式寫入 /* char[] buf = new char[1024]; int temp = 0; while ((temp = r.read(buf)) != -1) { w.write(new String(buf,0,temp)); } */ } catch (Exception e) { e.printStackTrace(); }finally{ //分別判斷是否空指針引用,而後關閉流 if(r != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } if(w != null) { try { w.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 字符流,利用字符流的緩衝區來進行文本文件的複製 */ public static void fun9(){ String doc = "D:"+File.separator+"test.txt"; String copy ="D:"+File.separator+"test2.txt"; FileReader r = null; FileWriter w = null; //建立緩衝區的引用 BufferedReader br = null; BufferedWriter bw = null; try { r = new FileReader(doc); w = new FileWriter(copy); //建立緩衝區對象 //將須要提升效率的FileReader和FileWriter對象放入其構造函數內 //固然,也可使用匿名對象的方式 br = new BufferedReader(new FileReader(doc)); br = new BufferedReader(r); bw = new BufferedWriter(w); String line = null; //讀取行,直到返回null //readLine()方法只返回換行符以前的數據 while((line = br.readLine()) != null) { //使用BufferWriter對象的寫入方法 bw.write(line); //寫完文件內容以後換行 //newLine()方法依據平臺而定 //windows下的換行是\r\n //Linux下則是\n bw.newLine(); } }catch (IOException e) { e.printStackTrace(); } finally { //此處再也不須要捕捉FileReader和FileWriter對象的異常 //關閉緩衝區就是關閉緩衝區中的流對象 if(br != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } if(bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }