逐行读取文件,逐行写入文件

时间:2014-10-28 00:36:24   收藏:0   阅读:168

java的io流相关的类实在是“太丰富”了,搞得有选择困难症的人有点无从下手。当然本菜鸟对java的io也是了解的不是很清楚,习惯用InputStrem,OutSteam的相关子类来处理二进制流如图片,用Reader,Writer相关的子类处理字符流如文本文件。为了提高效率使用了缓冲机制等。感觉缓冲机制多有于字符流,不知道二进制有那些缓冲机制,真的不了解得google一下。感觉博客园上的文章整体比csdn的文章的水平要高。而且看到博客园上的文章都质量很高,相比之下本菜鸟写的就会想起那句话:Too young too naive!Ok,标题就是这篇笔记的内容,代码如下:

 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 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!