码迷,mamicode.com
首页 > 编程语言 > 详细

java 文件按行读写

时间:2014-06-04 16:32:14      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

bubuko.com,布布扣
 1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.File;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 
 8 public class Demo001 {
 9 
10     public static void writeFile(String strSrc, String dest) {
11         FileWriter writer = null;
12         FileReader reader = null;
13         BufferedReader br = null;
14         BufferedWriter bw = null;
15 
16         try {
17             File file = new File(dest);
18             if (!file.exists()) {
19                 file.createNewFile();
20             }
21             
22             // 这里的true,是以追加的方式写
23             writer = new FileWriter(dest, true);
24 
25             reader = new FileReader(strSrc);
26 
27             String str = null;
28 
29             br = new BufferedReader(reader);
30 
31             bw = new BufferedWriter(writer);
32 
33             while ((str = br.readLine()) != null) {
34                 StringBuffer sb = new StringBuffer("");
35 
36                 sb.append(str + "\n");
37                 bw.write(sb.toString());
38             }
39 
40             br.close();
41             reader.close();
42 
43             bw.close();
44             writer.close();
45 
46         } catch (IOException e) {
47             e.printStackTrace();
48         }
49     }
50 
51     public static void main(String[] args) {
52         Demo001.writeFile("D:/hw31a.txt", "D:/hw31c.txt");
53         Demo001.writeFile("D:/hw31b.txt", "D:/hw31c.txt");
54     }
55 }
bubuko.com,布布扣

 

java 文件按行读写,布布扣,bubuko.com

java 文件按行读写

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/ahcc08/p/3764742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!