按行讀取
阿新 • • 發佈:2022-04-17
fileInputStream是位元組byte型
FileWriter 是位元組char型
補充案例(按行讀取)
package com.IO; import java.io.*; public class Line { public static void main(String[] args) { File file = new File("D://IDEA/Demo.txt"); File file1 = new File("D://IDEA/DemoCopy.txt"); try { FileReader fileInputStream = new FileReader(file); FileWriter fileOutputStream =new FileWriter(file1);//FileWriter/字元char級fileInputStream是位元組byte型 BufferedReader bufferedInput = new BufferedReader(fileInputStream); BufferedWriter bufferedOutput = new BufferedWriter(fileOutputStream); //建立快取佇列 /* main:1 char []chars = new char[1024*6]; int read = bufferedInput.read(chars); while (read!=-1){ bufferedOutput.write(chars,0,read); read = bufferedInput.read(chars); }*/ /* main:按行讀取 String str =bufferedInput.readLine(); while (str!=null){ bufferedOutput.write(str); bufferedOutput.newLine(); str =bufferedInput.readLine(); }*/ bufferedOutput.close(); bufferedInput.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }