串行化--深度复制

Posted king-boy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了串行化--深度复制相关的知识,希望对你有一定的参考价值。

  技术图片

技术图片

技术图片

 技术图片

技术图片

技术图片

package day;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.junit.jupiter.api.Test;

/**
 * DataInputStream+DataOutputStream
 * @author ASUS
 *
 */
public class DataiostreamDemo 
    /**
     * 写入
     * @throws Exception 
     */
    @Test
    public void write() throws Exception 
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream dos=new DataOutputStream(baos);
        dos.writeByte(-1);
        dos.close();
        baos.close();
        
    
package day;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.junit.jupiter.api.Test;

/*
 * 深度复制,复制的是整个对象图
 */
public class DeepCopyDemo 
    /**
     * 串行化过程
     * @throws Exception
     */
    @Test
    public void seria() throws Exception 
        PersonDemo p=new PersonDemo();
        Son s=new Son();
        Dog d=new Dog();
        
        //设置关联关系
        s.setPerson(p);
        d.setSon(s);
        
        //串行化
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:/arch/3.txt"));
        oos.writeObject(d);
        oos.close();
    
    /**
     * 反串行化过程
     * @throws Exception
     */
    @Test
    public void deSeria() throws Exception 
        
        //串行化
        ObjectInputStream oos=new ObjectInputStream(new FileInputStream("d:/arch/3.txt"));
        Dog d=(Dog)oos.readObject();
        oos.close();
        System.out.println(d.getSon());
    
    
    /**
     * 使用ByteArrayInputStream+ByteArrayOutputStream实现对象图的深度复制
     * @throws Exception 
     */
    @Test
    public void deeplyCopyinBAOS() throws Exception 
        PersonDemo p= new PersonDemo();
        Son s=new Son();
        Dog d= new Dog();
        
        //设置关联
        s.setPerson(p);
        d.setSon(s);
        
        //串行化过程
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(baos);
        oos.writeObject(d);
        oos.close();
        baos.close();
        
        //
        byte[] bytes=baos.toByteArray();
        //反串行化过程
        ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        d=(Dog)ois.readObject();
        ois.close();
        bais.close();
        
        System.out.println(d.getSon().getName());
    
    /**
     * 持续写入
     * @throws Exception 
     */
    @Test
    public void continusWrite() throws Exception 
        PersonDemo p= new PersonDemo();
        p.setName("tom");
        
        Son s=new Son();
        s.setName("jerry");
        
        Dog d= new Dog();
        d.setName("yellow");
        
        //串行
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(baos);
        oos.writeObject(p);
        oos.writeObject(s);
        oos.writeObject(d);
        oos.close();
        baos.close();
        
        byte[] bytes=baos.toByteArray();
        //反串行
        ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        PersonDemo p2=(PersonDemo)ois.readObject();
        Son s2=(Son)ois.readObject();
        Dog d2=(Dog)ois.readObject();
        ois.close();
        bais.close();
        
        System.out.println(p2.getName()+" "+s2.getName()+" "+d2.getName());
    

class PersonDemo implements Serializable
    private String name;

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    
    


class Son implements Serializable
    /**
     * 
     */
    private static final long serialVersionUID = -6634026021745509937L;
    private String name;
    //transient:临时的,暂时的
    private transient PersonDemo person;
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public PersonDemo getPerson() 
        return person;
    
    public void setPerson(PersonDemo person) 
        this.person = person;
    


class Dog implements Serializable
    /**
     * 
     */
    private static final long serialVersionUID = 607672335959009927L;
    
    private String name;
    private Son son;
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public Son getSon() 
        return son;
    
    public void setSon(Son son) 
        this.son = son;
    
package day;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class MulTheadCopyFile 
    static int i=0;
    static int block=0;
    public static void main(String[] args) throws Exception 
        //源文件
        File srcFile=new File("D:\\\\学习\\\\02大数据基础Hadoop 2.X\\\\大数据软件工具\\\\hadoop-2.5.0-cdh5.3.6-src.tar.gz");
        final RandomAccessFile srcRaf=new RandomAccessFile(srcFile, "r");
        //文件长度
        int srcLength=(int)srcFile.length();
        
        
        //目标文件
        File destFile=new File("d:/arch/hadoop-2.5.0-cdh5.3.6-src.tar.gz");
        final RandomAccessFile destRaf=new RandomAccessFile(destFile,"rw");
        destRaf.setLength(srcLength);
        
        //使用的线程数
        int count=3;
        
        //计算每个线程复制的文件块大小
        block= srcLength/count;
        
        //开启count个线程
        for(i=0;i<count;i++) 
            Thread t=new Thread() 
                public void run() 
                    int tmp=i;
                    int start=tmp*block;
                    int end=0;
                    //是否是最后一个线程
                    if(tmp!=(count-1)) 
                        end=(tmp+1)*block-1;
                    else 
                        end=srcLength-1;
                    
                    
                    //
                    try 
                        //定位文件指针
                        srcRaf.seek(start);
                        destRaf.seek(start);
                        
                        //
                        int bufsize=end-start+1;
                        byte[] buf=new byte[bufsize];
                        destRaf.write(buf);
                        
                        System.out.println(tmp+"over");
                     catch (IOException e) 
                        e.printStackTrace();
                    //
                
            ;
            t.start();
            t.join();
        
        Thread.sleep(1000);
        System.out.println("over");
    
package day;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.jupiter.api.Test;

public class ObjectSerializeDemo 
    /**
     * 串行化javabean
     * @throws Exception
     */
    @Test
    public  void serializePerson() throws Exception 
        //一个对象
        Person p =new Person("tom", 12);
        //fos
        FileOutputStream fos=new FileOutputStream("d:/arch/p.data");
        
        //通过文件输出流构造对象输出流
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        
        //序列化过程
        oos.writeObject(p);
        
        //关闭流
        oos.close();
        fos.close();
        System.out.println("over");
            
    
    /**
     * 反串行化javabean
     * @throws Exception
     */
    @Test
    public  void deSerialize() throws Exception 
        FileInputStream fis = new FileInputStream("d:/arch/p.data");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Person p=(Person)ois.readObject();
        ois.close();
        fis.close();
        System.out.println(p.getName());
            
    
package day;

import java.io.Serializable;

/**
 * 自定义javabean
 * @param age
 */
public class Person implements Serializable
    /**
     * 
     */
    private static final long serialVersionUID = -1288177377250822246L;
    private String name;
    private int age;
    
    
    public Person() 
        
    
    public Person(String name, int age) 
        super();
        this.name = name;
        this.age = age;
    
    
    
    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;
    
    
    

 

以上是关于串行化--深度复制的主要内容,如果未能解决你的问题,请参考以下文章

IT十八掌作业_java基础第十五天_IO串行化/深度复制

IT十八掌作业_java基础第十五天_IO串行化/深度复制

串行化对象(深度复制)

DAY16第十六天Socket套接字学习笔记

java串行化

php面向对象之把对象串行化