现在的位置: 首页 > 综合 > 正文

java IO之FileReader和FileWriter

2012年09月16日 ⁄ 综合 ⁄ 共 1215字 ⁄ 字号 评论关闭
 1 package com.io.test;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 
 8 import org.junit.Test;
 9 
10 public class TestFileReaderAndWriter {
11     @Test
12     public void testFileReader() {
13         try {
14             FileReader fr = new FileReader("E:/1.txt");
15             int ln = 0;
16             while ((ln = fr.read()) != -1) {
17                 System.out.print((char)ln);
18             }
19             fr.close();
20         } catch (FileNotFoundException e) {
21             e.printStackTrace();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }
26     @Test
27     public void testFileWriter(){
28         try {
29             FileReader fr = new FileReader("E:/1.txt");
30             FileWriter fw = new FileWriter("E:/3.txt");
31             int ln = 0;
32             while((ln = fr.read()) != -1){
33                 fw.write((char)ln);
34             }
35             fr.close();
36             fw.close();
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         }
42     }
43 }

抱歉!评论已关闭.