IO流操作文件
FileReader FileReader类从InputStreamReader类继承而来。该类按字符读取流中数据。可以通过以下几种构造方法创建需要的对象。(输入流 节点流 字符流 )
在给定从中读取数据的 File 的情况下创建一个新 FileReader。
1 public FileReader (File file)
在给定从中读取数据的 FileDescriptor 的情况下创建一个新 FileReader。
1 public FileReader (FileDescriptor fd)
在给定从中读取数据的文件名的情况下创建一个新 FileReader。查看源码得知,当fileName不为空时,会调用new file(fileName)创建出一个file对象,从而简化代码。
1 public FileReader (String fileName)
)
将的hello.txt文件内容读入程序中,并输出到控制台
标准写法
FileWriter FileWriter 类从 OutputStreamWriter 类继承而来。该类按字符向流中写入数据。可以通过以下几种构造方法创建需要的对象。(输出流 字符流 节点流)
在给出 File 对象的情况下构造一个 FileWriter 对象。
1 public FileWriter (File file)
在给出 File 对象的情况下构造一个 FileWriter 对象。
1 public FileWriter (File file, boolean append)
构造与某个文件描述符相关联的 FileWriter 对象。
1 public FileWriter (FileDescriptor fd)
在给出文件名的情况下构造 FileWriter 对象,它具有指示是否挂起写入数据的 boolean 值。
1 public FileWriter (String fileName, boolean append)
File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
append:如果 append 参数为 true,则将字节写入文件末尾处,从原有文件中继续写入,相当于追加信息。如果 append 参数为 false, 则写入文件开始处即对原有文件的覆盖。默认为false
FileReader和FileWriter的共同使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 import org.junit.Test;import java.io.*;public class FileReaderWriterTest { public static void main (String[] args) { File file = new File("hello.txt" ); System.out.println(file.getAbsolutePath()); File file1 = new File("day09\\hello.txt" ); System.out.println(file1.getAbsolutePath()); } @Test public void testFileReader () { FileReader fr = null ; try { File file = new File("hello.txt" ); fr = new FileReader(file); int data; while ((data = fr.read()) != -1 ) { System.out.print((char ) data); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fr != null ) { fr.close(); } } catch (IOException e) { e.printStackTrace(); } } } @Test public void testFileReader1 () { FileReader fr = null ; try { File file = new File("hello.txt" ); fr = new FileReader(file); char [] cbuf = new char [5 ]; int len; while ((len = fr.read(cbuf)) != -1 ) { String str = new String(cbuf, 0 , len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null ) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testFileWriter () { FileWriter fw = null ; try { File file = new File("hello1.txt" ); fw = new FileWriter(file, true ); fw.write("I have a dream!\n" ); fw.write("you need to have a dream!\n" ); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null ) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testFileReaderFileWriter () { FileReader fr = null ; FileWriter fw = null ; try { File srcFile = new File("hello.txt" ); File destFile = new File("hello2.txt" ); fr = new FileReader(srcFile); fw = new FileWriter(destFile); char [] cbuf = new char [5 ]; int len; while ((len = fr.read(cbuf)) != -1 ) { fw.write(cbuf, 0 , len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fw != null ) fw.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fr != null ) fr.close(); } catch (IOException e) { e.printStackTrace(); } } } }