二进制文件的读写&序列化和反序列化

二进制文件的读写&序列化和反序列化1.二进制文件的读写

二进制文件的读写&序列化和反序列化_第1张图片

1.二进制文件:音频,视频,图片文件

DataInputStream:需要FileInputStream做参数
*
DataOutputStream:需要FileOutputStream做参数

packagecn.com.abner.IO.picObjiect;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//C:\Users\Administrator\Desktop\新建文件夹\name.jpg复制到C:\Users\Administrator\Desktop\新建文件夹\pic
public class picremoveDemo {
     public static void main(String[] args) {
          //输入流
          DataInputStream a=null;
          FileInputStream b=null;
          //输出流
          DataOutputStream c=null;
          FileOutputStream d=null;
          try {
              b=new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹\\name.jpg");
              a=new DataInputStream(b);
              
              d=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹\\pic\\newname.jpg");
              c=new DataOutputStream(d);
              
              int temp;
              try {
                   while((temp=a.read())!=-1) {
                   c.write(temp);
                   }
              } catch (IOException e) {
                   e.printStackTrace();
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }finally {
              try {     
                   d.close();
                   c.close();
                   a.close();
                   b.close();
              } catch (IOException e) {
                   e.printStackTrace();
              }
          }         
     }
}

2.序列化和反序列化

* 

对象的读写
*
序列化:对象–>流中
*
对象对应的类必须实现Serializable接口,否则不能被序列化
*
ObjectOutputStream:需要FileOutputStream做参数
*
方法:writeObject(Object)
*
反序列化:流中–>对象
*
ObjectInputStream:需要FileInputStream做参数
*
方法:readObject( ) 【返回值为Object,需要强制转换】

packagecn.com.abner.IO.picObjiect;
import java.io.Serializable;
public class Student implements Serializable {
     private String name;
     private int age;
     private transient String stuID;
     
     public Student() {
          
     }
     public Student(String name, int age, String stuID) {
          this.name = name;
          this.age = age;
          this.stuID = stuID;
     }
     public String getName() {
          return name;
     }
     public void setName(String name) {
          this.name = name;
     }
     public int getAge() {
          return age;
     }
     public void setAge(int age) {
          this.age = age;
     }
     public String getStuID() {
          return stuID;
     }
     public void setStuID(String stuID) {
          this.stuID = stuID;
     }
     
     
}
packagecn.com.abner.IO.picObjiect;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
//将学生类序列化和反序列化
public class ObjectRemove {
     public static void main(String[] args) {
          Student stu=new Student("Abner",25,"123");
          
          ObjectOutputStream a=null;
          FileOutputStream b=null;
          
          ObjectInputStream c=null;
          FileInputStream d=null;
          try {
              b=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹\\student.txt");
              try {
                   a=new ObjectOutputStream(b);
                   a.writeObject(stu);
                   
                   d=new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹\\student.txt");
                   c=new ObjectInputStream(d);
                   
                   try {
                        Student stu1=(Student)c.readObject();
                        
                        System.out.println(stu1.getName()+" "+stu1.getAge()+" "+stu1.getStuID());      //Abner 25 null
                        System.out.println(stu1.getName()+" "+stu1.getAge());                               //Abner 25
                   } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                   }
              } catch (IOException e) {
                   e.printStackTrace();
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
     }
}

3.transient关键字

定义属性加关键字,该属性取消序列化

你可能感兴趣的:(基础总结)