流的概念:内存与存储设备之间数据传输的通道,数据借助流传输
read()
close()
:节约资源write()
close()
:节约资源FileInputStream:
read(byte[] b)
:从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1//1 创建 FileInputStream,并指定路径 FileInputStream fis = new FileInputStream("F:\\a.txt"); //2 读文件 //读一个字节 int data = 0; while ((data = fis.read()) != -1) { System.out.print((char) data); } //3.关闭 fis.close();
使用byte[]读取多个字节
FileInputStream fis = new FileInputStream("F:\\a.txt"); byte[] b = new byte[3]; int count = fis.read(b);//read()范围值为读取字节的个数 System.out.println(new String(b)); System.out.println(count); int count2 = fis.read(b); System.out.println(new String(b)); System.out.println(count2); fis.close();
读取结果:为 fjk
和 ljk
,默认读取的方法在读取结束时不会覆盖未读取的内容,需要对其进行限制:
int count5 = fis.read(b); System.out.println(new String(b,0,count5));//l System.out.println(count5);//1
使用循环读取:
FileInputStream fis = new FileInputStream("F:\\a.txt"); byte[] b = new byte[3]; int count = 0; while((count = fis.read(b))!= -1){ System.out.println(new String(b,0,count)); } fis.close();
write(byte[] b)
:一次写多个字节,将b数组中所有字节,写入输出流true
FileOutputStream fileOutputStream = new FileOutputStream("F:\\b.txt",true);
//1 创建文件输出流对象 FileOutputStream fileOutputStream = new FileOutputStream("F:\\b.txt"); //2. 写入文件 fileOutputStream.write(97); fileOutputStream.write('c'); fileOutputStream.write('s'); //3. 关闭 fileOutputStream.close(); System.out.println("执行完毕");
//1 创建文件输出流对象 FileOutputStream fileOutputStream = new FileOutputStream("F:\\b.txt"); //2. 写入文件 String s = "Hello,world"; fileOutputStream.write(s.getBytes());//执行写入操作的时候覆盖源文件 //3. 关闭 fileOutputStream.close(); System.out.println("执行完毕");
字节流可以复制任何文件
//1. 创建流 //文件字节输入流 FileInputStream fileInputStream = new FileInputStream("F:\\wall.jpg"); //文件字节输出流 FileOutputStream fileOutputStream = new FileOutputStream("F:\\1.jpg"); //2. 边读边写 byte[] b = new byte[1024]; int count = 0; while ((count=fileInputStream.read(b))!=-1){ fileOutputStream.write(b,0,count); } //3. 关闭 fileInputStream.close(); fileOutputStream.close(); System.out.println("复制完毕");
//1. 创建BufferedInputStream //把字节流传入缓冲流 FileInputStream fileInputStream = new FileInputStream("F:\\a.txt"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //2. 读取 //读取到缓冲区,然后再读取到文件中,缓冲区大小维8192 int data = 0; while((data=bufferedInputStream.read())!=-1){ System.out.print((char)data); } //3.关闭缓冲区 bufferedInputStream.close();
//1. 创建BufferedInputStream //把字节流传入缓冲流 FileInputStream fileInputStream = new FileInputStream("F:\\buffer.txt"); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //2. 读取 //自己创建缓冲区 byte[] b = new byte[1024]; int count = 0; while((count = fileInputStream.read(b))!=-1){ System.out.println(new String(b,0,count)); } //3.关闭缓冲区 bufferedInputStream.close();
flush
刷新才能将缓冲区的内容写入硬盘中flush
进行写入close
缓冲流时,会调用 flush
进行把缓冲区的内容写入硬盘中flush
操作可以提高效率//1.创建字节输出缓冲流 FileOutputStream fileOutputStream = new FileOutputStream("F:\\buffer.txt"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); //2.写入文件 for (int i = 0; i < 9; i++) { //仅写入缓冲区中,没有写入硬盘中,缓冲区满了会自动调用flush bufferedOutputStream.write("hello,world!\r\n".getBytes()); //刷新到硬盘中,提高效率 bufferedOutputStream.flush(); } //3.close(内部调用flush) //即使不进行刷新,关闭缓冲区时也会写入到硬盘中 bufferedOutputStream.close();
序列化是将数据类型和类进行写入,要序列化类必须实现 Serializable
接口
public class Student implements Serializable{}
//1.创建对象流 FileOutputStream fileOutputStream = new FileOutputStream("F:\\stu.bin"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); //2.序列化(写入操作) Student stu1 = new Student("小王",29); objectOutputStream.writeObject(stu1);//需要进行序列化,需要使类实现序列化的接口 //3.关闭 objectOutputStream.close(); System.out.println("序列化完毕");
反序列化:读取文件,再重构成对象
一个对象仅能反序列化一次
//1. 创建对象流 FileInputStream fileInputStream = new FileInputStream("F:\\stu.bin"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); //2. 读取文件(反序列化) Student stu = (Student)objectInputStream.readObject(); //如果读取两次会出现异常,每个对象反序列化仅能进行一次 // Student stu2 = (Student)objectInputStream.readObject(); System.out.println(stu.toString()); //3. 关闭 objectInputStream.close();
序列化:
private static final long serialVersionUID = xxxL;
transient
(瞬间的)修饰属性,使属性不被序列化序列化:
//序列化多个对象 Student stu1 = new Student("小王",29); Student stu2 = new Student("林",12); ArrayList<Student> list = new ArrayList<>(); list.add(stu1); list.add(stu2); objectOutputStream.writeObject(list);//需要进行序列化,需要使类实现序列化的接口
反序列化:
//反序列化 ArrayList<Student> list = (ArrayList<Student>) objectInputStream.readObject(); System.out.println(list.toString());