文章目录 [隐藏]
FileReader、BufferedReader继承自读取字符流的抽象类Reader,子类必须实现的方法只有 read(char[], int, int) 和 close()。;
BufferedWriter、FileWriter继承自写入字符流的抽象类Writer,子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。
一。FileReader读文本文件
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 |
public static void main(String[] args) throws IOException { URL url = FileReaderTest.class.getResource("/com/cuiweiyou/iotest/text.txt"); // \\项目\\src-bin\\包路径\文件 System.out.println("==========每次读1个字符==============="); FileReader fr = new FileReader(url.getPath()); // 关联已存在的目标文件 int character = 0; // 缓存字符的变量 while ((character = fr.read()) != -1) { // 读取结束返回-1,否则返回读到的字符ASCII码 System.out.println((char) character); // ASCII码转字符 } fr.close(); // 关闭流。和输入流不同,这里没有flush System.out.println("==========每次读1个字符==============="); fr = new FileReader(url.getPath()); while (true) { int read = fr.read(); if (read == -1) break; System.out.println((char) read); } fr.close(); System.out.println("==========每次读数组长度的字符=========="); fr = new FileReader(url.getPath()); char[] cbuf = new char[1024]; // 创建一个长度为1024的字符数组进行缓存 int length = 0; // 读取字符尽可能填满数组。返回读到的字符个数,读取结束返回-1 while ((length = fr.read(cbuf)) != -1) { String string = new String(cbuf, 0, length); // 读取字符数组为字符串,从0索引开始读,读取长度为num System.out.println("数量:" + length + ",内容:" + string); } fr.close(); } |
二。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 |
/** * 注意IO异常处理的模板流程 */ public static void main(String[] args) { // 声明成员。FileWriter:写入字符文件类,用来写入字符到文件的便捷类 FileWriter fw = null; // 1.在try中初始化,写数据 try { fw = new FileWriter("k:/test.txt"); // 目标文件,在一个不存在的盘符上创建。已有文件覆盖,除非添加第二个参数true fw.write("sdfghjk\r\nnm1234567"); // 写入文件的字符 fw.flush(); // 刷新缓存 } // 2.捕捉写文件的异常 catch (IOException e) { // .字符串输出流,可以用其回收在字符串缓冲区中的输出来构造字符串 StringWriter sw = new StringWriter(); // ..格式化字符输出流,true: println、printf 或 format 方法将刷新输出缓冲区 PrintWriter pw = new PrintWriter(sw, true); // ...将此 throwable 及其追踪输出到指定的 PrintWriter e.printStackTrace(pw); System.out.println("写文件异常" + sw.toString()); } // 3.关闭字符流 finally { // 3.1.尝试关闭 try { // 如果上面的1.有异常被2.捕捉到,fw就不能初始化 if (fw != null) { fw.close(); } } // 3.2.捕捉关闭流的异常 catch (IOException e) { e.printStackTrace(); } } } |
三。使用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 |
public static void main(String[] args) throws IOException { URL urlInput = FileReaderAndFileWriter.class.getResource("/com/cuiweiyou/iotest/textInput.txt");// 须已存在的源文件 URL urlOutput = FileReaderAndFileWriter.class.getResource("/com/cuiweiyou/iotest/textOutput.txt");// 目标文件 System.out.println("==========每次读写一个字符复制文件=========="); FileReader fr = new FileReader(urlInput.getPath()); // 字符文件输入流 FileWriter fw = new FileWriter(urlOutput.getPath()); // 字符文件输出流 int character = 0; // 每次缓存一个字符。都是英文,一个字符即一个字节 while ((character = fr.read()) != -1) { // 每次读一个字符,结束为-1 fw.write(character); // 立即将读到的这个字符写到输出流 } fw.flush(); // 刷新流 fw.close(); // 关闭流 fr.close(); // 关闭流 System.out.println("==========每次读写一个数组长度复制文件=========="); fr = new FileReader(urlInput.getPath()); fw = new FileWriter(urlOutput.getPath()); char[] chars = new char[1024]; // 长度1024的缓存字符数组 int length = 0; // 每次读取的字符数。如果读满了chars,则返回1024;否则读了几个就返回几 while ((length = fr.read(chars)) != -1) { // 读取结束返回-1 fw.write(chars, 0, length); // 立即将读到的这些字符写到输出流(读满的字符数组,从0索引开始写,读了多少写多少) } //fw.flush(); // 刷新流 fw.close(); // 关闭流,首先隐式的flush fr.close(); // 关闭流 } |
四。缓冲字符输入流BufferedReader读文本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static void main(String[] args) throws IOException { // 缓冲区字符输入流(字符输入流) BufferedReader br = new BufferedReader(new FileReader("text.txt")); // int read() 读取单个字符。返回:读入的字符,如果已到达流末尾,则返回 -1 // int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。返回:读取的字符数,如果已到达流末尾,则返回 -1 // 行文本缓存 String line = null; // 读取一个文本行。标识字符:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。读取结束返回null while ((line = br.readLine()) != null) { System.out.println(line); } // 关闭流 br.close(); } |
五。缓冲字符输出流BufferedWriter写文本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 缓冲区。提高效率 * 要使用缓冲区,首先要有流对象 */ public static void main(String[] args) throws IOException { // 缓冲字符输出流(字符输出流 out)。为out对象提供缓冲,底层的写功能由这个out完成 BufferedWriter bw = new BufferedWriter(new FileWriter("text.txt")); // /项目/text.txt // 写入缓冲区 bw.write("dfghjfgh76543223456"); // 提供了跨平台的 newLine()方法换行 bw.newLine(); bw.write(";lkajgkhlj;k"); // 刷新缓冲区,写到文件 bw.flush(); // 关闭缓冲区,即关闭了字符输出流。不必再fw.close(); bw.close(); } |
六。使用缓冲功能的BufferedReader和BufferedWriter复制文本文件
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 |
public static void main(String[] args) { // 缓冲区输入输出流 BufferedReader br = null; BufferedWriter bw = null; try { // 缓冲区指定流对象关联文件 br = new BufferedReader(new FileReader("test.txt")); // 须已存在,否则抛异常 bw = new BufferedWriter(new FileWriter("test_copy.txt")); String line = null; // 缓冲区输入流读行,直到结束 while ((line = br.readLine()) != null) { // 缓冲区输出流将刚读到的写出 bw.write(line); // 换行 bw.newLine(); // 刷新缓冲区 bw.flush(); } } catch (IOException e) { throw new RuntimeException("有异常状况"); } finally { try { if (bw != null) bw.close(); // 关闭输出流 } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); // 关闭输入流 } catch (IOException e) { e.printStackTrace(); } } } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1034.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设