• transient


    package other;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class TestSix<E> {
        
        public static void main(String[] args) {
    
            /**
             *http://www.importnew.com/21517.html
             *  被 transient修饰域不能序列化
             * 
             */
            
            
            try {
                
                User user =  new User();
                user.setName("Ziv");
                user.setPassword("111");
                
                
                String path="D:/test/test.txt";
                create(path); 
                
                ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(path));
                ob.writeObject(user);
                ob.flush();
                ob.close();
                
                ObjectInputStream in =  new ObjectInputStream(new FileInputStream(path));
                Object object =  in.readObject();
                in.close();
                
                System.out.println(object);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
        
        
        public static void create(String path){
            try {
                String fileContent = path.substring(0, path.lastIndexOf("/"));    
                File content =new File(fileContent);
                //创建目录
                if (!content.exists()) {
                    content.mkdirs();
                }
                //创建文件
                File file =new File(path);
                if (!file.exists()) {
                    file.createNewFile();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    class  User implements Serializable{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L; 
    
        private String name;
        
        private transient String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
        
        /*@Override
        public String toString() {
            return "name:"+ this.name+"password:"+this.password;
        }*/
        
    }
  • 相关阅读:
    上机练习3
    上机练习2
    上机练习1
    第一次作业
    第二次作业
    第一次作业
    第二次作业(4)
    第二次作业(3)
    第二次作业(2)
    第二次作业(1)
  • 原文地址:https://www.cnblogs.com/lxh520/p/8360254.html
Copyright © 2020-2023  润新知