字符流:读写字符的
顶级父类是Reader和Writer
操作流程
在Java中IO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:
- 使用File类打开一个文件
- 通过字节流或字符流的子类,指定输出的位置
- 进行读/写操作
- 关闭输入/输出
思维导图
相关练习代码。
1 /** 2 * Date : 2017/7/1 3 * Author : Hsj 4 * Description :字符流知识总结 5 */ 6 public class Demo2 { 7 8 9 //可以读取字符 10 @Test 11 public void fun() throws IOException { 12 FileReader fr = new FileReader("zzz.txt"); 13 int read = fr.read(); 14 System.out.println(read); 15 char c = (char) read; 16 System.out.println(c); 17 fr.close(); 18 } 19 20 //连续读取文字 21 @Test 22 public void fun2() throws IOException { 23 FileReader fr = new FileReader("zzz.txt"); 24 int len; 25 while ((len = fr.read()) != -1) { 26 System.out.print((char) len); 27 } 28 fr.close(); 29 } 30 31 //文字写入 32 @Test 33 public void fun3() throws IOException { 34 FileWriter fw = new FileWriter("kk.txt"); 35 fw.write("我是胡少君,你在哪里"); 36 fw.close(); 37 } 38 39 //字符拷贝 40 @Test 41 public void fun4() throws IOException { 42 FileReader fr = new FileReader("kk.txt"); 43 FileWriter fw = new FileWriter("xxx.txt"); 44 int len; 45 while ((len = fr.read()) != -1) { 46 fw.write(len); 47 } 48 fr.close(); 49 fw.close();//有一个小的缓冲区不关流写不进去 50 } 51 52 //自定义数组的拷贝 53 @Test 54 public void fun5() throws IOException { 55 FileReader fr = new FileReader("kk.txt"); 56 FileWriter fw = new FileWriter("kj.txt"); 57 char[] chars = new char[1024 * 8]; 58 int len; 59 while ((len = fr.read(chars)) != -1) { 60 fw.write(chars, 0, len); 61 } 62 fr.close(); 63 fw.close(); 64 65 } 66 67 @Test 68 //缓冲字符流 69 public void fun6() throws IOException { 70 BufferedReader br = new BufferedReader(new FileReader("kj.txt")); 71 BufferedWriter bw = new BufferedWriter(new FileWriter("hsj.txt")); 72 int len; 73 while ((len = br.read()) != -1) { 74 bw.write(len); 75 } 76 br.close(); 77 bw.close(); 78 } 79 80 //读取一行 81 @Test 82 public void fun7() throws IOException { 83 BufferedReader br = new BufferedReader(new FileReader("hsj.txt")); 84 String s; 85 while ((s = br.readLine()) != null) {//一行一行的读取文件 86 System.out.print(s); 87 } 88 br.close(); 89 } 90 91 //添加换行符 92 @Test 93 public void fun8() throws IOException { 94 BufferedReader br = new BufferedReader(new FileReader("hsj.txt")); 95 BufferedWriter bw = new BufferedWriter(new FileWriter("lyb.txt")); 96 String s; 97 while ((s = br.readLine()) != null) { 98 bw.write(s); 99 bw.newLine();//换行 100 } 101 br.close(); 102 bw.close(); 103 104 } 105 106 //文本反转 107 @Test 108 public void fun9() throws IOException { 109 List<String> slist = new ArrayList<>(); 110 BufferedReader br = new BufferedReader(new FileReader("hsj.txt")); 111 BufferedWriter bw = new BufferedWriter(new FileWriter("tran.txt")); 112 String s; 113 while ((s = br.readLine()) != null) { 114 slist.add(s); 115 } 116 117 for (int i = slist.size() - 1; i >= 0; i--) { 118 bw.write(slist.get(i)); 119 bw.newLine(); 120 } 121 bw.close(); 122 br.close(); 123 } 124 125 // LineNumberReader的使用 126 @Test 127 public void fun10() throws IOException { 128 LineNumberReader lnr = new LineNumberReader(new FileReader("hsj.txt")); 129 String line; 130 while ((line = lnr.readLine()) != null) { 131 System.out.println(lnr.getLineNumber() + ":" + line); 132 133 } 134 lnr.close(); 135 } 136 }