zoukankan      html  css  js  c++  java
  • 第二十六节(对象流,File类)

    对象流可以将 Java 对象转换成二进制写入磁盘,这个过程通常叫做序列化,并且还可
    
    以从磁盘读出完整的 Java 对象,而这个过程叫做反序列化。
    
    对象流主要包括:ObjectInputStream 和 ObjectOutputStream
    
    
    
    如何实现序列化和反序列化
    
    
    如果实现序列化该类必须实现序列化接口 java.io. Serializable , 该接口没有任何方法, 该接口
    
    只是一种标记接口,标记这个类是可以序列化的
    
    
    /*
        对象流:
        
            对象流可以将java对象转换成二进制写入磁盘,这个过程叫做“序列化”
            还可以从磁盘读取完整的Java对象,这个过程叫 “反序列化”
            
            包括:ObjectInputStream 和 ObjectOutputStream
    
        java.io.Serializable 
    
    */
    
    import java.io.*;
    
    public class ObjectStreamTest01{
        
        public static void main(String[] ags){
            
            ObjectOutputStream oos = null;
            
            try{
                FileOutputStream fos = new FileOutputStream("C:\work\Java\arry.txt");
                
                oos = new ObjectOutputStream(fos); 
                
                Student stu = new Student();
                stu.name = "ming太帅了,没道理不爱你 !";
                
                oos.writeObject(stu);
            } catch(FileNotFoundException e){
                e.printStackTrace();    
            } catch(IOException e){
                e.printStackTrace();    
            } finally{
                try{
                    if(oos != null){
                        oos.close();    
                    }
                } catch(IOException e){
                    e.printStackTrace();        
                }
            }
        }
    
    }
    
    // 实现序列化接口
    class Student implements Serializable{
        String name;
    }
    
    
    //////////////////////////////////////////////////////////
    
    
    
    /*
        对象流:
        
            对象流可以将java对象转换成二进制写入磁盘,这个过程叫做“序列化”
            还可以从磁盘读取完整的Java对象,这个过程叫 “反序列化”
            
            包括:ObjectInputStream 和 ObjectOutputStream
    
        java.io.Serializable 
    
    */
    
    import java.io.*;
    
    public class ObjectStreamTest02{
        
        public static void main(String[] ags){
            
            ObjectInputStream ois = null;
            
            try{
                
                FileInputStream fis = new FileInputStream("C:\work\Java\arry.txt");
                
                ois = new ObjectInputStream(fis);
                
                // 反序列化
                Student stu = (Student)ois.readObject();
                
                System.out.println(stu.name);
                
                
            }catch(FileNotFoundException e){
                e.printStackTrace();    
            }catch(IOException e){
                e.printStackTrace();
            }catch(ClassNotFoundException e){
                e.printStackTrace();    
            } finally{
                try{
                    if(ois != null){
                        ois.close();    
                    }
                }catch(IOException e){
                    e.printStackTrace();            
                }
            }
            
        }
    
    }
    
    // 实现序列化接口
    class Student implements Serializable{
        String name;
    }
    File 提供了大量的文件操作:
    
    删除文件,修改文件,得到文件修改日期,建立目录、列表文件等
    
    
    /*
        java.io.File;
    
            1. File类与流无关,不能通过该类完成文件的读和写
            2. File是文件和目录路径名的抽象的表现形式
    
        File代表的是硬盘上的Directory 和 file
    
    */
    
    import java.io.*;
    
    public class FileTest01{
        
        public static void main(String[] args){
            
            // path 相对路径
            File f1 = new File("PrintStreamTest02.java");
            
            File f2 = new File("C:\work\Java\course\vip\JavaSE-Course-Arry\chapter26\PrintStreamTest02.java");
            
            File f3 = new File("C:\work\Java");
            
            System.out.println(f1.exists());//输出true
            System.out.println(f2.exists());//true
            System.out.println(f3.exists());//true
            
            File f4 = new File("c:\yyy");
            System.out.println(f4.exists());
            
            // 如果不存在在创建
            if(!f4.exists()){
                // 创建目录
                f4.mkdir();
                try{
                    // 创建文件
                    f4.createNewFile();
                }catch(IOException e){
                    e.printStackTrace();
                }                
                
            }
            
            File f5 = new File("c:\a\b\c\d");
            if(!f5.exists()){
                f5.mkdirs();    // C:acd
            }
            
        }
    
    }
    
    /////////////////////////////////////////////////////////////////////////
    
    
    
    
    /*
        File类中的部分方法
    
    */
    
    import java.io.*;
    import java.util.*;
    import java.text.*;
    
    public class FileTest02{
        
        public static void main(String[] args){
            
            // 获取绝对路径
            File f1 = new File("FileTest01.java");
            
            String absolutePath = f1.getAbsolutePath();
            
            System.out.println("绝对路径:" + absolutePath);
            
            // 获取文件名
            System.out.println("文件名:"+f1.getName());
            
            // 获取父路径
            File f2 = new File("C:\work\Java\course\vip\JavaSE-Course-Arry\chapter26");
            String parentPath = f2.getParent();
            System.out.println(parentPath);
            
            // 判断该File 是文件File还是目录Directory
            System.out.println(f1.isDirectory());//false
            System.out.println(f1.isFile());//true
            
            // 获取文件的最后一次修改时间
            Date d = new Date(f1.lastModified());
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(d));
            
            // 获取文件的长度(字节数)
            System.out.println("文件的长度:" + f1.length());
            
            File f3 = new File("C:\work\Java\course\vip\JavaSE-Course-Arry\chapter26");
            File[] fs = f3.listFiles();
            
            //遍历
            for(File f : fs){
                if(f.getAbsolutePath().endsWith(".java")){
                    System.out.println("绝对路径 : " + f.getAbsolutePath());    
                }
            }
            
            
    
            
            
        }
    
    }
  • 相关阅读:
    涂抹mysql笔记-安装mysql
    使用Oracle DBLink进行数据库之间对象的访问操作
    mysqldump备份与恢复笔记
    linux6下源码安装mysql5.6
    虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法
    linux添加zabbix service并开机自动启动
    理解Linux系统负荷load average
    oracle体系结构理解
    C#中Abstract和Virtual[转载]
    WPF后台写ControlTemplate总结
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/4234329.html
Copyright © 2011-2022 走看看