I/O Stream-related classes in Java are "too rich", and people with difficult choices cannot start. Of course, this cainiao does not know much about Java I/O. I am used to using the nputstrem and outsteam subclasses to process binary streams. I use reader, child classes related to writer are used to process streams such as text files. The buffer mechanism is used to improve efficiency. I feel that the buffer mechanism is mostly a dirty stream. I don't know what buffer mechanism the binary system has. I can't solve it by Google. I feel that the posts in the blog garden are generally higher than those in csdn. In addition, I can see that all the articles in the blog Park are of high quality. In contrast, what I wrote by cainiao will remind me of the sentence: Too young too naive! OK. The title is the content of this note. The Code is as follows:
1 package io.newline; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.PrintWriter;10 11 public class CopyLine {12 public static void main(String[] args) {13 try {14 BufferedReader br = new BufferedReader(new FileReader("F:/a/s.txt"));15 16 PrintWriter out = new PrintWriter(new BufferedWriter(17 new FileWriter("F:/a/t.txt", true)));18 19 String line = null;20 21 while ((line = br.readLine()) != null) {22 System.out.println(line);23 out.println(line); //向t.txt写入一行24 }25 if (br != null) {26 br.close();27 }28 if (out != null) {29 out.close();30 }31 32 } catch (FileNotFoundException e) {33 e.printStackTrace();34 } catch (IOException e) {35 e.printStackTrace();36 }37 38 }39 40 }
Reads files row by row and writes files row by row