1. 程式人生 > >I/O流復制文本

I/O流復制文本

lock package read fileread 控制 str code nds 決定

package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class IO流 { /** * @param args */ public static void main(String[] args) { bufferedWR(); } private static void show(File file) { File[] fileList
= file.listFiles(); for (File f : fileList) { if (f.isFile()) { if (f.getName().endsWith(".txt")) System.out.println(f.getName()); } else { show(f); } } } private static void delete(File file) { File[] fileList
= file.listFiles(); for (File f : fileList) { if (f.isFile()) { f.delete(); } else { delete(f); } } file.delete(); } /** * 抽象類 字節流 讀取 InputStream 字節流 輸出 OutputStream * * 字符流 讀取 Reader 字符流 輸出 Writer * * 實現類 BateArrayOutputStream FileOutputStream OutputStream FileOutputStream * 構造方法 FileOutputStream(File file) FileOutputStream(String path) * * @throws */ // 字符串寫入文件 /** * 追加寫入 FileOutputStream fos = new FileOutputStream("a.txt",true); 換行 \r\n */ public static void strToFile() { // 創建字節輸出流對象 String str = "hello"; try { // File file = new File("D:\\a.txt"); // FileOutputStream fs = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream("a.txt", true); fos.write("hello \r\nhello".getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 讀取文件內容,輸出到控制臺 */ public static void readFromFile() { File file = new File("a.txt"); FileInputStream fis = null; try { fis = new FileInputStream(file); byte[] bys = new byte[1024]; int len = 0; while ((len = fis.read()) != -1) { System.out.print((char) len); } } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 復制文本文件 從a.txt 到 b.txt * */ public static void copyByte() { try { FileInputStream is = new FileInputStream("a.txt"); FileOutputStream os = new FileOutputStream("b.txt"); int len = 0; byte[] bys = new byte[1024]; while ((len = is.read(bys)) != -1) { os.write(bys, 0, len); } os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } finally { } } /** * BufferedOutputStream 緩沖輸出 */ public static void bufferedOutputDemo() { try { FileOutputStream fos = new FileOutputStream("a.txt"); FileInputStream fis = new FileInputStream("b.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedInputStream bis = new BufferedInputStream(fis); int len = 0; byte[] bys = new byte[1024]; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * OutputStreamWriter(file,character) 是轉換流 字節+編碼方式 * * InputStreamWriter(file,character) 是轉換流 字節+編碼方式 * * flush()方法 刷新緩沖 */ /** * 字符流復制文件 */ public static void copyZiFULiu() { try { OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("a.txt")); InputStreamReader oiw = new InputStreamReader(new FileInputStream( "c.txt")); int len = 0; char[] b = new char[1024]; while ((len = oiw.read(b)) != -1) { osw.write(b, 0, len); osw.flush(); } osw.close(); oiw.close(); } catch (Exception e) { e.printStackTrace(); } } /** * FileWriter = FileOutputStream + 編碼 FileReader = FileInputStream + 編碼 */ public static void copyRW() { try { FileReader reader = new FileReader("a.txt"); FileWriter writer = new FileWriter("d.txt"); int len = 0; char[] c = new char[1024]; while ((len = reader.read(c)) != -1) { writer.write(c, 0, len); writer.flush(); } writer.close(); reader.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * BufferedWriter(Writer out) * BufferedReader(Reader read) * 字符緩沖流的特殊方法: * newLine():根據系統來決定換行符 * readLine(): 一次讀取一行數據 */ public static void bufferedWR(){ try { // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("e.txt"))); BufferedWriter bw = new BufferedWriter(new FileWriter("e.txt")); // BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt"))); BufferedReader br = new BufferedReader(new FileReader("a.txt")); // int len = 0; String s = null; char[] c = new char[1024]; while((s= (br.readLine()))!= null){ // bw.write(c,0,len); // bw.flush(); bw.write(s); bw.newLine(); bw.flush(); } bw.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } }

I/O流復制文本