节点流——FileReaderWriter(使用)
1 import java.io.File; 2 3 import java.io.FileNotFoundException; 4 5 import java.io.FileReader; 6 7 import java.io.FileWriter; 8 9 import org.junit.Test; 10 11 public class TestFileReaderWriter { 12 13 //读入 14 15 @Test 16 17 public void testFileRead() throws Exception { 18 19 File f = new File("hello3.txt"); 20 21 FileReader fr = new FileReader(f); 22 23 char[] c = new char[24]; 24 25 int len; 26 27 while ((len = fr.read(c)) != -1) { 28 29 String str = new String(c, 0, len); 30 31 System.out.println(str); 32 33 } 34 35 fr.close(); 36 37 } 38 39 //复制,实现文本文件的复制,对应非文本文件只能使用字节流 40 41 //非文本文件:视频,音频,图片,二进制文件等 42 43 @Test 44 45 public void testCopy() throws Exception { 46 47 File out = new File("hello3.txt"); 48 49 FileReader fr = new FileReader(out); 50 51 File in = new File("hello.txt"); 52 53 FileWriter fw = new FileWriter(in); 54 55 int len; 56 57 char[] c = new char[24]; 58 59 while ((len = fr.read(c)) != -1) { 60 61 fw.write(c, 0, len); 62 63 } 64 65 fw.close(); 66 67 } 68 69 }
博客园地址:https://www.cnblogs.com/lixiuming521125/