標籤:fileinput fileread 代碼 system throw close 冗餘 源碼 ade
檔案讀寫
如果在代碼中寫入大量的資料,會增加代碼的冗餘度,通過讀取檔案的方式,可以精簡代碼,便於資料的修改和代碼的維護
IO流的分類:位元組流和字元流
字元流
#javapackage study1;import org.testng.annotations.Test;import java.io.*;public class IODemo { @org.junit.Test public void test() throws IOException { //如果檔案不存在,會自動建立 FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt")); fw.write("hello world\n"); fw.write("123\n"); /** * 寫入檔案後,未執行其他動作,寫入的內容不會儲存 * 寫入檔案後,close流,會自動儲存檔案。 * 寫入資料較多是,使用write寫入後,使用flush儲存寫入的內容,降低對系統壓力 */ fw.flush(); fw.write(123);//此處寫入的是ascii碼值 fw.flush(); fw.close();//釋放流資源 } @org.junit.Test public void test2() { FileWriter fw= null ; try{ fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt")); }catch (IOException e){ e.printStackTrace(); }finally{ try{ fw.close();//此處釋放流資源,必須要先初始化 }catch (IOException e){ e.printStackTrace(); } } } @org.junit.Test public void append() throws IOException{ //追加內容,而非覆蓋 FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"),true); fw.write("hello python\n"); fw.flush(); fw.close(); } @org.junit.Test public void test_read() throws IOException{ //讀取檔案 FileWriter fw = null; fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt")); fw.write("abcd"); fw.flush(); fw.close(); FileReader fr = null; fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt")); System.out.println(fr.read());//輸出字元的ASCII碼值,每執行一次就讀取1次 System.out.println((char)fr.read()); System.out.println(fr.read()); System.out.println(fr.read()); System.out.println(fr.read()); System.out.println(fr.read());//沒有內容,輸出-1 fr.close(); } @org.junit.Test public void test_read2() throws IOException{ //讀取單個字元 FileWriter fw = null; fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt")); fw.write("abcd"); fw.flush(); fw.close(); FileReader fr = null; fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt")); int i=0; while((i= fr.read())!=-1){ /** * 擷取字元的ascii碼值,並根據ascii碼值強轉為字元 */ System.out.println((char)i); } fr.close(); } @org.junit.Test public void test_read3() throws IOException{ //讀取字元數組,一次讀取2個字元 FileWriter fw = new FileWriter(new File("Data/b.txt")); fw.write("java"); char [] cha = {‘a‘,‘b‘,‘c‘}; fw.write(cha); fw.close(); FileReader fr = new FileReader(new File("Data/b.txt")); int i = 0; char [] ch = new char[2];//一次讀取2個字元 while((i=fr.read(ch))!=-1){ System.out.print(i+"\t"); System.out.println(ch); /** * System.out.println(i+"\t"+new String(ch)); * 和int類型同時使用,不加new String,會返回雜湊碼[[email protected] * 加上new String和上面2行輸出結果一致 * 2 ja * 2 va * 2 ab * 1 cb 最後1次只裝入了1個字元,只將a替換為c,b並未替換 */ } } @org.junit.Test //單個字元的寫入 public void test_cp() throws IOException{ FileReader fr = null; FileWriter fw = null; fr = new FileReader(new File("Data/b.txt")); fw = new FileWriter(new File("Data/b1.txt")); int i = 0; while((i=fr.read())!=-1){ fw.write((char)i); } fw.close(); fr.close(); } @org.junit.Test //字元數組的寫入 public void test_cp1() throws IOException{ FileReader fr = null; FileWriter fw = null; fr = new FileReader(new File("Data/b.txt")); fw = new FileWriter(new File("Data/b2.txt")); char [] ch = new char[2]; int i = 0; while((i=fr.read(ch))!=-1){ fw.write(ch,0,i); } fw.close(); fr.close(); } @org.junit.Test public void test_out() throws IOException{ //位元組輸出資料流 FileOutputStream out = null; out = new FileOutputStream(new File("Data/c.txt")); out.write("abcdefgh".getBytes()); out.close(); } @org.junit.Test public void test_in() throws IOException{ //位元組輸入資料流 FileInputStream in = null; in = new FileInputStream(new File("Data/c.txt")); int i = 0; while((i=in.read())!=-1){ System.out.println((char)i); } in.close(); } @org.junit.Test public void test_in_char() throws IOException{ FileInputStream in = new FileInputStream(new File("Data/c.txt")); int i = 0; byte [] bt = new byte[10]; while((i=in.read(bt))!=-1){ System.out.print(i+"\t"+new String(bt)); /** * System.out.println(bt);返回雜湊碼[[email protected] * 長度不滿10位,會用空位補齊 */ } } @org.junit.Test public void test_IO() throws IOException{ //將某網頁的源碼儲存為檔案 FileInputStream in = new FileInputStream(new File("Data/d.html")); FileOutputStream out = new FileOutputStream(new File("Data/d1.html")); int i = 0; byte [] bt = new byte[1024]; while((i=in.read(bt))!=-1){ out.write(bt,0,i); } out.close(); in.close(); System.out.println("success"); }}
java study檔案讀寫