java IO 之BufferedReader和BufferedWriter
1 package com.io.test;
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
10 import org.junit.Test;
11
12 public class TestBufferedReader {
13
14 @Test
15 public void test() {
16 try {
17 BufferedReader bufReader = new BufferedReader(
18 new FileReader(
19 "/Users/Jet/Documents/workspace/IOTest/test/com/io/test/TestBufferedInputStream.java"));
20 BufferedWriter bufWriter = new BufferedWriter(
21 new FileWriter(
22 "/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java"));
23 String str = "";
24 while ((str = bufReader.readLine()) != null) {
25 System.out.println(str);
26 bufWriter.write(str);
27 }
28 bufWriter.flush();
29 bufReader.close();
30 bufWriter.flush();
31 } catch (FileNotFoundException e) {
32 e.printStackTrace();
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36
37 }
38
39 }
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
10 import org.junit.Test;
11
12 public class TestBufferedReader {
13
14 @Test
15 public void test() {
16 try {
17 BufferedReader bufReader = new BufferedReader(
18 new FileReader(
19 "/Users/Jet/Documents/workspace/IOTest/test/com/io/test/TestBufferedInputStream.java"));
20 BufferedWriter bufWriter = new BufferedWriter(
21 new FileWriter(
22 "/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java"));
23 String str = "";
24 while ((str = bufReader.readLine()) != null) {
25 System.out.println(str);
26 bufWriter.write(str);
27 }
28 bufWriter.flush();
29 bufReader.close();
30 bufWriter.flush();
31 } catch (FileNotFoundException e) {
32 e.printStackTrace();
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36
37 }
38
39 }