java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址

a.txt 文本内容如下:

name=user
age=34
image=aa.PNG
url=E:\\

读入序列化:

java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址
package cn.com.test05;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Person3 implements Serializable{
    private static final long serialVersionUID = 1L;
    String  name;
    int  age;
    String image;
    byte[] imageB;
    String url;
    
    
    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 getImage() {
        return image;
    }


    public void setImage(String image) {
        this.image = image;
    }


    public byte[] getImageB() {
        return imageB;
    }


    public void setImageB(byte[] imageB) {
        this.imageB = imageB;
    }


    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }


    public String toString(){
        return "我叫"+name+"=="+age+"===="+image;
    }
}
public class t07 {

    public static void main(String[] args) throws Exception {
        String s="da.fsagj";
        Person3 p=new Person3();
        get(p);
       System.out.println(p);
       outObject(s, p);
    }
    
     public static void outObject(String s,Person3 p) throws Exception{
         File f= new File(p.getUrl()+s);
         ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f));
         oos.writeObject(p);
         oos.close();
     }
     public static byte[] getImage(String s) throws Exception{
         File f= new File("F:\\"+s);
         BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
         byte[] b=new byte[(int) f.length()];
         bis.read(b);
         bis.close();
         return b;
         
     }
    public static void get(Person3 p) throws Exception{
        File f= new File("F:\\a.txt");
        BufferedReader br= new BufferedReader(new FileReader(f));
        p.setName(getValue(br.readLine()));
        p.setAge(Integer.parseInt(getValue(br.readLine())));
        p.setImage(getValue(br.readLine()));
        p.setUrl(getValue(br.readLine()));
        p.setImageB(getImage(p.getImage()));
        br.close();
    }
    public static String getValue(String s){
        return s.split("=")[1];
        
    }
    
}
java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址

 

反序列化写出:

java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址
package cn.com.test05;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;

public class t08 {

    public static void main(String[] args) throws Exception {
        String s="da.fsagj";
           Person3 p = getObject(s);
           System.out.println(p);
           out(p);
    }
    public static void outImage(Person3 p) throws Exception{
         File f= new File(p.getUrl()+p.getImage());
         BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(f));
         bos.write(p.getImageB());
         bos.close();
    }
    public static void out(Person3 p) throws Exception{
         File f= new File(p.getUrl()+"ab.txt");
         BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(f));
         bos.write(p.toString().getBytes());
         outImage(p);
         bos.close();
    }
    public static Person3 getObject(String s) throws Exception{
         File f= new File("E:\\"+s);
         ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
        Person3 p = (Person3)ois.readObject();
        ois.close();
        return p;
     }
}
java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址

 

 

java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址,布布扣,bubuko.com

java 读取文件,内容方置Person 序列化到磁盘,在读入程序并写到另外地址

上一篇:C语言中static变量详解


下一篇:JAVA笔记7-Object类之toString方法和equals方法