(IO)字符文件读写操作-FileReader&FileWriter
(IO)字符文件读写操作---FileReader&FileWriter
文本文件都是字符数据,因此使用字符流进行操作。
可以通过缓冲区来提高对文件的读写效率。
读写文本文件
package com.gc.stream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo { public static void main(String[] args) { testFileWriter(); testFileReader(); testFileReaderWithBuffer(); } //手动设置缓冲区来提高效率 private static void testFileReaderWithBuffer() { FileReader fr = null; try { fr = new FileReader("temp\\text.txt"); //字符缓冲区 char[] cbuf = new char[1024]; //记录每次读取到cbuf缓冲区中的字符个数 int len = 0; //read(cbuf),一次读取cbuf.length个字符,如果数组满,则阻塞 //阻塞后,取出数组中的字符 //再次读取,重新存入数组,从0角标开始覆盖之前的数据 //最后读取到文件末尾,返回-1,循环结束 while((len=fr.read(cbuf))!=-1) { //从缓冲区中取出本次所读取的字符 System.out.print(new String(cbuf, 0, len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fr!=null) try { fr.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } /** * 使用FileReader读取文件中的文本数据,没有使用缓冲区,一次读1个字符就阻塞,效率低! */ private static void testFileReader() { FileReader fr = null; try { fr = new FileReader("temp\\text.txt"); int ch = 0; //read()方法1次读1个字符,即2个字节。[1个中文字符占2个字节] //文件内容的结尾处用-1表示 while((ch=fr.read())!=-1) { System.out.print((char)ch); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fr!=null) try { fr.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } /** * 使用FileWriter将字符数据写入到文件 */ private static void testFileWriter() { //字符流写文件 FileWriter fw = null; try { //如果是相对路径,则自动相对于该项目的根目录 File parent = new File("temp"); if(!parent.exists()) parent.mkdir(); File f = new File(parent,"text.txt"); if(!f.exists()) f.createNewFile(); //是否追加写入 boolean append = false; fw = new FileWriter(f,append); fw.write("你好hello stream!"); //换行 fw.write(System.lineSeparator()); fw.write("new Line"); //刷新缓冲区 fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if(fw!=null) try { //关闭流(关闭动作会自动刷新缓冲区) fw.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }
复制文本文件
package com.gc.file; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; //手动设置缓冲区,提高读写效率 public class CopyFile { private static final int BUFFER_SIZE = 1024;//缓冲区大小 public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader("temp\\text.txt"); fw = new FileWriter("temp\\copy_text.txt"); //建立一个临时容器,用来缓存每次读取到的字符 char[] buf = new char[BUFFER_SIZE]; //记录每次读取到字符缓冲区的字符个数 int len = 0; while((len=fr.read(buf))!=-1) { //将本次读取到的字符从缓冲区取出,并写入到文件 fw.write(buf, 0, len); } //fw.flush();//刷新与否都可以,因为close()会自动刷新缓冲区 } catch(IOException e) { throw new RuntimeException("文件读写发生错误"); } finally { //最后,关闭流 try { if(fr!=null) fr.close(); if(fw!=null) fw.close(); } catch (IOException e) { throw new RuntimeException("关闭流失败"); } } } }