流
-
概念:内存与存储设备之间传输数据的通道
-
数据借助流进行传输
流的分类
-
按方向
-
输入流(input):将 存储设备 中的内容读入到 内存 中
-
输出流(output): 将 内存 中的内容写入到 存储设备 中
-
-
按单位
-
字节流:以字节为单位,可以读写所有数据
-
字符流:以字符为单位,只能读写文本数据
-
-
按功能
-
节点流:具有实际传输数据的读写功能
-
过滤流:在节点流的基础之上增强功能
-
字节流
字节流的父类(抽象类)
-
Input Stream:字节输入流
-
public int read (){}
-
public int read (byte[] b){}
-
public int read (byte[] b , int off , int len){}
-
-
OutputStream:字节输出流
-
public void write(int n){}
-
public void write(byte[] b){}
-
public void write (byte[] b ,int off , int len){}
-
文件字节流
-
FileInputStream:
-
public int read (byte[] b)//从流中读取多个字节,将读到的内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
-
public static void main(String[] args) throws IOException { //创建FileInputStream,并指定文件路径 FileInputStream fis = new FileInputStream("D:\\桌面\\测试\\aaa.txt"); //判断文件是否读完,当文件读完时,返回-1 //单个字节读取文件 int data = 0; while ((data = fis.read()) !=-1){ System.out.print((char)data); } //一次读取多个字节 byte[] b = new byte[512]; //一次读入512字节内容 int count = fis.read(b); System.out.println(new String(b,0,count)); System.out.println(count); int count2 = fis.read(b); System.out.println(new String(b,0,count2)); System.out.println(count2); //使用循环一次堕入多个字节 byte[] b = new byte[128]; int count = 0; while ((count = fis.read(b))!=-1){ System.out.println(new String(b,0,count)); System.out.println(count); } //关闭流 fis.close(); System.out.println(); System.out.println("文件传输完毕"); }
-
-
FileOutputStream
-
public void write(byte[] b)//一次写多个字节,将b数组中所有字节写入到输出流
-
public static void main(String[] args) throws IOException { //创建文件输出流 //FileOutputStream fos = new FileOutputStream("D:\\桌面\\测试\\bbb.txt"); //每次写入会对源文件进行覆盖 FileOutputStream fos = new FileOutputStream("D:\\桌面\\测试\\bbb.txt",true); //写入的时候会对源文件的内容进行追加 //写入文件 //单个字节写入 /* fos.write(97); fos.write('c');*/ //一次写入多个字节 String string = "我爱Java,Java是世界第1."; fos.write(string.getBytes()); fos.close(); System.out.println("执行结束"); }
案例,使用文件输入流和文件输出流实现文件内容复制
public class Demo3 { public static void main(String[] args) throws IOException { //创建字节输入流 FileInputStream fis = new FileInputStream("D:\\桌面\\测试\\aaa.txt"); //创建字节输出流 FileOutputStream fos = new FileOutputStream("D:\\桌面\\测试\\ccc.txt"); //设置读取文件字节数组 byte[] b = new byte[1024]; //count 获取本次读入的字节长度,当返回值为-1时,代表结束 int count = 0; while ((count=fis.read(b))!=-1){ String s = new String(b,0,count); fos.write(s.getBytes()); } fis.close(); fos.close(); System.out.println("复制完成"); } }
文件缓冲流
-
缓冲流:BufferedOutputStream/BufferedIntputStream
-
提高IO效率,减少访问磁盘的次数
-
数据存储在缓冲区中,flush是将缓冲区的内容写入到文件中,也可以直接close
-
public static void main(String[] args) throws IOException { //创建字节输入流 FileInputStream fis = new FileInputStream("D:\\桌面\\测试\\aaa.txt"); //创建字节缓冲流 BufferedInputStream bis = new BufferedInputStream(fis); int count = 0; while ((count = bis.read())!=-1){ System.out.println((char)count); } }
public class Demo5 { public static void main(String[] args) throws IOException { //创建字节输入流 FileOutputStream fos = new FileOutputStream("D:\\桌面\\测试\\buffer.txt"); //创建字节缓冲输入流 BufferedOutputStream bos = new BufferedOutputStream(fos); String s = "我爱Java