- 概念
- 读写数据的状态向流水一样从一端向另一端。
- 根据读写单位的不同 分为字节流和字符流,字节流可以读写任意文件,,字符流只能读写文本文件。
- 按照读写数据方向的不同,分为输入流,输出流
-
按照读写数据的基本单位不同,分为 字节流 和 字符流。其中字节流主要指以字节为单位进行数据读写的流,可以读写任意类型的文件。其中字符流主要指以字符 (2 个字节 ) 为单位进行数据读写的流,只能读写文本文件。按照读写数据的方向不同,分为 输入流 和 输出流(站在程序的角度)。其中输入流主要指从文件中读取数据内容输入到程序中,也就是读文件。其中输出流主要指将程序中的数据内容输出到文件中,也就是写文件。按照流的角色不同分为节点流和处理流。其中节点流主要指直接和输入输出源对接的流。其中处理流主要指需要建立在节点流的基础之上的流。
-
体系结构:
-
FileWriter 类
- 概念: 主要用于将文本内容写入到文本文件
- 常用的方法:
- close 自带刷新功能
-
package com.lagou.task17; import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static void main(String[] args) { //1.构造FileWrite类型的对象与文件夹关联 //若文件不存在,该流会自动创建新文件。 //若文件存在,则该流会清空文件中的原有内容; FileWriter fw = null; try { //fw = new FileWriter("d:/a.txt"); //以追加的方式创建文件并关联对象; // 若文件不存在,则自动创建新的空文件,若文件存在,则保留原有数据内容。 fw = new FileWriter("d:/a.txt",true); //2.通过流对象写入数据内容 fw.write('a'); System.out.println("写入数据成功"); } catch (IOException e) { e.printStackTrace(); } finally { //3.关闭流对象并释放有关的资源 if (null != fw) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
FileReader 类
- 概念:从文被文件读取文本数据内容
- 常用的方法:
-
-
package com.lagou.task17; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { FileReader fr = null; try { //1.创建FileReader类型的对象与文件关联 fr = new FileReader("d:/a.txt"); //2.读取文件的内容并打印 /*int res = fr.read(); System.out.println("读取的数据内容是:" + (char)res);*/ /* int res = 0; while ((res = fr.read())!= -1){ System.out.println("读取的数据内容是:" + (char)res); }*/ //准备一个字符数组保存读取到的内容 char[] cArr = new char[5]; //期望读满字符数组中的一本分空间,也就是读取三个字符放入字符数组cArr下标从1开始的位置中 /* int res = fr.read(cArr, 1, 3); System.out.println("读取字符的个数是:" + res); for (char c:cArr ) { System.out.println("读取的数据内容是:" + (char)c); }*/ //期望读满整个字符数组cArr int res = fr.read(cArr); System.out.println("读取字符的个数是:" + res); for (char c:cArr ) { System.out.println("读取的数据内容是:" + (char)c); } } catch (IOException e) { e.printStackTrace(); } finally { //3.关闭流对象并释放相关的资源 if (null != fr) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
File Reader类与FileWriter类实现文件内容的拷贝(文件字节流)
package com.lagou.task17; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCharCopyTest { public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; try { //1.创建FileReader类型的对象与文件向关联额、 fr = new FileReader("d:/a.txt"); //2.创建FileWriter类型的对象与文件相关联 fw = new FileWriter("d:/b.txt"); //3.不断地读取写入文件中的字符 System.out.println("正在玩命的拷贝:"); int res = 0; while ((res = fr.read()) != -1 ){ fw.write(res); } System.out.println("文件拷贝成功..."); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } if (fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
目录
FilleInputStream类
FileOutputStream类
BufferInputStream类
BufferOutputStream类
package com.lagou.task17; import java.io.*; public class BufferCharCopyTest { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; try { //1.创建BufferedReader类型的对象与文件 d:/a.txt关联 br = new BufferedReader(new FileReader("d:/a.txt")); //2.创建BufferedWriter类型的对象与文件 d:/b.txt关联 bw = new BufferedWriter(new FileWriter("d:/b.txt")); //3.不断地从输入流拷贝文件数据写入到输出流中 System.out.println("正在玩命拷贝中。。。。。。。"); String str = null; while ((str = br.readLine()) != null){ bw.write(str); } System.out.println("拷贝完成"); } catch (IOException e) { e.printStackTrace(); } finally { //4.关闭流对象并释放相关资源 if (null != bw) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
-
-