今天在测试IO流的使用的时候发现在reader读取文件之后,再向文件添加内容,再继续读文件,打印出的结果只能读取追加的文件。
如何才能重新读取呢?试了mark和reset,似乎会报异常。记在这以后看是否有解决方法。或者各位有什么看法也可以指教一下。
File newFile = new File("D:/io.txt"); FileReader readerStream = null; FileWriter writerStream = null; try { if (newFile.createNewFile()) { System.out.println("文件创建成功"); } else { System.out.println("文件创建已存在"); } readerStream = new FileReader(newFile); int read = -1; while ((read = readerStream.read()) != -1) { System.out.print((char) read); } writerStream = new FileWriter(newFile, true); writerStream.write("Tttttt"); writerStream.flush(); System.out.println("写入完成,重新读取。。。"); BufferedReader bReader = new BufferedReader(readerStream); System.out.println(bReader.readLine() + "--"); bReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { readerStream.close(); writerStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }