I/O之Writer-Reader

  1 import java.io.File;
  2 import java.io.FileNotFoundException;
  3 import java.io.FileReader;
  4 import java.io.FileWriter;
  5 import java.io.IOException;
  6 
  7 import org.junit.Test;
  8 
  9 public class RorW {
 10     // 测试 Reader Writer
 11     @Test
 12     public void test1() {
 13 
 14         // ---------------------创建测试文件---------------------
 15         File file = new File("E:/IO测试/测试.txt");
 16         if (!file.exists()) {
 17             file.getParentFile().mkdirs();
 18             try {
 19                 file.createNewFile();
 20             } catch (IOException e) {
 21                 e.printStackTrace();
 22             }
 23         }
 24 
 25         // ---------------------测试FileWriter-----------------------
 26         FileWriter fw = null;
 27         try {
 28             fw = new FileWriter(file);
 29             fw.write("这是测试输出流");
 30             // fw.write("这是测试输出流覆盖");
 31             // ==========================================================
 32             if (fw != null) {// FileWriter声明的引用,必须flush()和close()后才能重新赋值
 33                 try {
 34                     fw.flush();
 35                 } catch (IOException e) {
 36                     e.printStackTrace();
 37                 }
 38                 try {
 39                     fw.close();
 40                 } catch (IOException e) {
 41                     e.printStackTrace();
 42                 }
 43             }// FileWriter声明的引用,必须flush()和close()后才能重新赋值
 44                 // ==========================================================
 45             fw = new FileWriter(file, true);
 46             fw.write("+这是测试追加输出流");
 47         } catch (IOException e1) {
 48             e1.printStackTrace();
 49         } finally {
 50             if (fw != null) {
 51                 try {
 52                     fw.flush();
 53                 } catch (IOException e) {
 54                     e.printStackTrace();
 55                 }
 56                 try {
 57                     fw.close();
 58                 } catch (IOException e) {
 59                     e.printStackTrace();
 60                 }
 61             }
 62         }
 63 
 64         // ---------------------测试FileReader---------------------
 65         FileReader fr = null;
 66         try {
 67             fr = new FileReader(file);
 68             // ============单次只读取一个字符,返回int型编码
 69             int i = fr.read();
 70             System.out.println(i);
 71             System.out.println((char) i);// 将int型编码强制转换字符输出
 72             // ============
 73             while ((i = fr.read()) != -1) {// 当返回-1时表示到达文件末尾;
 74                 System.out.print((char) i);
 75             }
 76             System.out.println();// 为了输出好看
 77         } catch (FileNotFoundException e) {
 78             e.printStackTrace();
 79         } catch (IOException e) {
 80             e.printStackTrace();
 81         } finally {
 82             // 关闭资源
 83             if (fr != null) {
 84                 try {
 85                     fr.close();
 86                 } catch (IOException e) {
 87                     e.printStackTrace();
 88                 }
 89             }
 90         }
 91 
 92     }
 93 
 94     @Test
 95     public void test2() {
 96         try {
 97             FileReader fr = new FileReader("E:/IO测试/测试.txt");
 98             char[] cbuf = new char[1024];
 99             if (fr.ready()) {
100                 fr.read(cbuf);
101                 System.out.println(cbuf);
102             }
103         } catch (FileNotFoundException e) {
104             e.printStackTrace();
105         } catch (IOException e) {
106             e.printStackTrace();
107         }
108     }
109 
110 }

 

posted @ 2016-11-10 10:56  WJY_JY  阅读(171)  评论(0编辑  收藏  举报