class IoTotal { public static void main(String[] args) { } //字元輸入輸出資料流 一個一個讀 public static void total01() throws IOException { FileReader fr = new FileReader("1.txt"); FileWriter fw = new FileWriter("2.txt"); int a; while ((a = fr.read()) != -1) { fw.write(a); } fr.close(); fw.close(); } //字元輸入輸出資料流 一個字元數組讀 public static void total02() throws IOException { FileReader fr = new FileReader("1.txt"); FileWriter fw = new FileWriter("2.txt"); char[] arr = new char[1024 * 4]; int len; while ((len = fr.read(arr)) != -1) { fw.write(arr, 0, len); } fw.close(); fr.close(); } //高速字元輸入輸出資料流 一個一個讀 public static void total03() throws IOException { FileReader fr = new FileReader("1.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("2.txt"); BufferedWriter bw = new BufferedWriter(fw); int a; while ((a = br.read()) != -1) { bw.write(a); } br.close(); bw.close(); } //高速字元輸入輸出資料流 一個字元數組讀 public static void total04() throws IOException { FileReader fr = new FileReader("1.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("2.txt"); BufferedWriter bw = new BufferedWriter(fw); char[] arr = new char[1024 * 4]; int len; while ((len = br.read(arr)) != -1) { bw.write(arr, 0, len); } bw.close(); br.close(); } //字元緩衝流 裝飾方法 此處唯寫特有方法 public static void total05() throws IOException { BufferedReader br = new BufferedReader(new FileReader("1.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("2.txt")); String str = null; while ((str = br.readLine()) != null) { bw.write(str); bw.newLine(); } br.close(); bw.close(); } //位元組輸入輸出資料流 一個一個讀 public static void total06() throws IOException { FileInputStream fin = new FileInputStream("1.txt"); FileOutputStream fout = new FileOutputStream("2.txt"); int a; while ((a = fin.read()) != -1){ fout.write(a); } fin.close(); fout.close(); } //位元組輸入輸出資料流 一個位元組數組的讀 public static void total07() throws IOException { FileInputStream fin = new FileInputStream("1.txt"); FileOutputStream fout = new FileOutputStream("2.txt"); byte[] arr = new byte[1024*4]; int len; while ((len = fin.read(arr)) != -1){ fout.write(arr,0,len); } fin.close(); fout.close(); } //高速位元組輸入輸出資料流 一個一個讀 public static void total08() throws IOException { FileInputStream fin = new FileInputStream("1.txt"); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream("2.txt"); BufferedOutputStream bout = new BufferedOutputStream(fout); int a; while ((a = bin.read()) != -1){ bout.write(a); } bin.close(); bout.close(); } //高速位元組輸入輸出資料流 一個位元組數組的讀 public static void total09() throws IOException { FileInputStream fin = new FileInputStream("1.txt"); BufferedInputStream bin = new BufferedInputStream(fin); FileOutputStream fout = new FileOutputStream("2.txt"); BufferedOutputStream bout = new BufferedOutputStream(fout); byte[] arr = new byte[1024*4]; int len; while ((len = bin.read(arr)) != -1){ bout.write(arr,0,len); } bin.close(); bout.close(); } //位元組->字元->位元組 public static void total10() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("1.txt")))); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("1.txt",true)))); String line = null; while ((line = br.readLine()) != null){ System.out.println(line); bw.write(line); bw.newLine(); } br.close(); bw.close(); /* int len = 0; char[] arr = new char[1024*4]; while ((len = br.read(arr)) != -1){ bw.write(arr,0,len); } bw.close(); br.close();*/ }}