FileOutputStream,BufferedOutputStream,对象的序列化与反序列化ObjectOutputStream

来源:互联网 发布:金山软件官网 编辑:程序博客网 时间:2024/06/06 07:08

(1)FileOutputStream:节点流,向文件写出字节的流

package day0825io;import java.io.FileOutputStream;import java.io.IOException;/** * 节点流:低级流 *  * 处理流:高级流 * 1:不能独立存在,构造方法通常会传入另一个流 * 2:用于处理另一个流 * 3:使用高级流的目的是为了简化读写操作 *  * OutputStream 字节输出流的父类 */public class FosDemo1 {    public static void main(String[] args) throws IOException {        //FileOutputStream:节点流,向文件写出字节的流        FileOutputStream fos = new FileOutputStream("fos.dat");        fos.write(97);        String str = "认真而不执着";        byte[] buf = str.getBytes("UTF-8");        fos.write(buf);        fos.close();    }}

(2)使用文件字节流对文件写操作

public class FosDemo2 {    public static void main(String[] args) throws IOException {        /**         * FileOutputStream支持一个重载的构造方法         * FileOutputStream(String str,boolean append)         * 第2个参数指定是否进行追加操作         * 若不追加,当前文件中所有内容都会被清除,然后写数据         *          * 注意这一点跟RandonAccessFile不同         */        FileOutputStream fos = new FileOutputStream("fos.dat",true);        fos.write(99);        fos.close();    }}

(3)FileInputStream:节点流,用于从文件中读取字节的流

package day0825io;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;//FileInputStream:节点流,用于从文件中读取字节的流------public class FisDemo3 {/** * fos.dat文件中的内容(utf-8编码,共20个字节): * a认真而不执着c */    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("fos.dat");        int i = fis.read();//读取一个字节        System.out.println(i);//97        byte[] buf = new byte[40];        int len = fis.read(buf);//实际读取到的字节量        System.out.println(len);//19        String str = new String(buf,"UTF-8");//读取到的buf转为String        System.out.println(str);//认真而不执着c    }}

BufferedOutputStream

package day0825io;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;public class BosDemo4 {    public static void main(String[] args) throws IOException {        FileOutputStream fos = new FileOutputStream("bos.txt");        BufferedOutputStream bos = new BufferedOutputStream(fos);        String str = "东方红太阳升";        byte[] buf = str.getBytes("UTF-8");        bos.write(buf);        /**         * flush()         * 强制将当前缓冲区的缓冲数据全部写出         * 无论缓冲区是否被装满         */        //bos.flush();        bos.close();//先flush,然后关闭内部流    }}

(5)用输入输出流实现文件的复制

package day0825io;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;//(5)用输入输出流实现文件的复制----public class FisFosCopy5 {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("src.jpg");        FileOutputStream fos = new FileOutputStream("copy.jpg");        /*        int d = -1;        while((d=fis.read())!=-1){            fos.write(d);        }        */        byte[] buf = new byte[10240];//10K        int len = -1;        while((len=fis.read(buf))!=-1){////len:实际读取到的字节量            fos.write(buf);        }        fis.close();        fos.close();    }}

(6)缓冲流(高级流):加快读写时间

package day0825io;//(6)缓冲流(高级流):加快读写时间------import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class BisBosCopy6 {    public static void main(String[] args) throws IOException {        FileInputStream fis = new FileInputStream("src.jpg");        BufferedInputStream bis = new BufferedInputStream(fis);        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg"));        int d = -1;        while((d=bis.read())!=-1){            //读取一个字节,当读完整个数组以后,再去读一组数据回来            bos.write(d);//只拿一个字节,当拿满数组时,一次性写出去        }        //java只关闭最外层的流即可,bis内部自己关闭里面的流        bis.close();        bos.close();    }}

Person类

package day0825io;import java.io.Serializable;import java.util.List;/** * 一个对象若想通过ObjectOutputStream进行序列化 * 那么该对象所属的类必须实现Serializable接口 *  * 该接口没有任何抽象方法 * 实现该接口仅仅用于标识当前类的实例可以被序列化 */public class Person implements Serializable{    /**     * 当类的属性增加或者修改了,若版本号不变     * 那么反序列化时尽量兼容现有版本     * 若版本号发生了改变,那么反序列化时会抛出异常     */    private static final long serialVersionUID = 1L;    private String name;    private int age;    //transient仅仅用于序列化及反序列化时被忽略    private transient char gender;    //private int salary;    private double other;    private List<String> otherInfo;    public Person() {}    public Person(String name, int age, char gender, double other,            List<String> otherInfo) {        this.name = name;        this.age = age;        this.gender = gender;        //this.salary = salary;        this.other = other;        this.otherInfo = otherInfo;    }    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 char getGender() {        return gender;    }    public void setGender(char gender) {        this.gender = gender;    }//  public int getSalary() {//      return salary;//  }////  public void setSalary(int salary) {//      this.salary = salary;//  }    public double getOther() {        return other;    }    public void setOther(double other) {        this.other = other;    }    public List<String> getOtherInfo() {        return otherInfo;    }    public void setOtherInfo(List<String> otherInfo) {        this.otherInfo = otherInfo;    }    public static long getSerialversionuid() {        return serialVersionUID;    }    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + ", gender=" + gender                +",other:"+other+  ", otherInfo=" + otherInfo + "]";    }}

(7)将对象序列化

package day0825io;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;/** * 1:序列化:将特定的数据结构转换为一组字节的过程 * 2:持久化:数据写入硬盘长久保存的过程 *  * 序列化与反序列化一般用于: * 1:传输 * 2:保存 */public class TestPerson7 {    public static void main(String[] args) throws IOException {        List<String> list = new ArrayList<String>();        list.add("上海");        list.add("汉族");        list.add("其他信息");        Person p = new Person("张三",22,'男',2000,list);        FileOutputStream fos = new FileOutputStream("p.obj");        ObjectOutputStream oos = new ObjectOutputStream(fos);        /**         * void writeObject(Object o)         * ObjectOutputStream提供的方法         * 可以将给定的对象转换为一组字节后写出         */        oos.writeObject(p);        oos.close();    }}

(8)将存储对象的文件 反序列化

package day0825io;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class OisDemo8 {    /**     * ObjectInputStream是一个高级流     * 可以将一组字节转换为对应的对象,用于对象的反序列化     * @param args     * @throws IOException     * @throws ClassNotFoundException     */    public static void main(String[] args) throws IOException, ClassNotFoundException {        FileInputStream fis = new FileInputStream("p.obj");        ObjectInputStream ois = new ObjectInputStream(fis);        Person p = (Person)ois.readObject();        System.out.println(p.toString());        ois.close();    }}
0 0
原创粉丝点击