• 串行化--深度复制


      

     

    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;
        }
        
    }    
  • 相关阅读:
    windows下 php-cgi.exe 0xc000007b 错误 阿星小栈
    call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败 阿星小栈
    PHP json_encode返回的json前端获取时出现unicode转码和反斜杠导致无法解析的解决办法
    Warring:POST Content-Length of 625523488 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 阿星小栈
    PHP数组分割成新数组 阿星小栈
    Laravel ajax请求419错误及解决办法(CSRF验证) 阿星小栈
    MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded... 阿星小栈
    Laravel框架发送邮件 阿星小栈
    PHP 导出Excel三种方式 阿星小栈
    mysql命令 出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  • 原文地址:https://www.cnblogs.com/King-boy/p/11045428.html
Copyright © 2020-2023  润新知