java序列化流和反序列化流  序列化流与反序列化流 打印流 commons-IO  工具类

 对象序列化流ObjectOutputStream

ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。

注意:只能将支持 java.io.Serializable 接口的对象写入流中

用于从流中读取对象的

操作流 ObjectInputStream    称为 反序列化流

用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流

特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。

代码演示:

public class ObjectStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /*
         * 将一个对象存储到持久化(硬盘)的设备上。
         */
        writeObj();//对象的序列化。
    }
    public static void writeObj() throws IOException {
        //1,明确存储对象的文件。
        FileOutputStream fos = new FileOutputStream("tempfile\obj.object");
        //2,给操作文件对象加入写入对象功能。
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //3,调用了写入对象的方法。
        oos.writeObject(new Person("wangcai",20));
        //关闭资源。
        oos.close();
    }
}
//    Person类
public class Person implements Serializable {
    private String name;
    private int age;
    public Person() {
        super();
    }
    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;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

 对象反序列化流ObjectInputStream

ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。支持 java.io.Serializable接口的对象才能从流读取。

public class ObjectStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        readObj();//对象的反序列化。
    }
    public static void readObj() throws IOException, ClassNotFoundException {
        
        //1,定义流对象关联存储了对象文件。
        FileInputStream fis = new FileInputStream("tempfile\obj.object");
        
        //2,建立用于读取对象的功能对象。
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        Person obj = (Person)ois.readObject();
        
        System.out.println(obj.toString());
        
    }
}

打印流

打印流根据流的分类:

  字节打印流  PrintStream

  字符打印流  PrintWriter

  方法:

void print(String str): 输出任意类型的数据,

void println(String str): 输出任意类型的数据,自动写入换行操作

  代码演示:

/* 
 * 需求:把指定的数据,写入到printFile.txt文件中
 * 
 * 分析:
 *     1,创建流
 *     2,写数据
 *     3,关闭流
 */
public class PrintWriterDemo {
    public static void main(String[] args) throws IOException {
        //创建流
        //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));
        PrintWriter out = new PrintWriter("printFile.txt");
        //2,写数据
        for (int i=0; i<5; i++) {
            out.println("helloWorld");
        }
        //3,关闭流
        out.close();
    }
}

打印流完成数据自动刷新

  构造方法:

  开启文件自动刷新写入功能

public PrintWriter(OutputStream out, boolean autoFlush)

public PrintWriter(Writer out, boolean autoFlush)

 代码演示:

/* 
 * 分析:
 *     1,创建流
 *     2,写数据
 */
public class PrintWriterDemo2 {
    public static void main(String[] args) throws IOException {
        //创建流
        PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true);
        //2,写数据
        for (int i=0; i<5; i++) {
            out.println("helloWorld");
        }
        //3,关闭流
        out.close();
    }
}

commons-IO  工具类

导入classpath

加入classpath的第三方jar包内的class文件才能在项目中使用

创建lib文件夹

将commons-io.jar拷贝到lib文件夹

右键点击commons-io.jar,Build Path→Add to Build Path

 FilenameUtils

 常用方法:

getExtension(String path):获取文件的扩展名;

getName(String filename):获取文件名;

isExtension(String fileName,String ext):判断fileName是否是ext后缀名;

 FileUtils

  常用方法:

readFileToString(File file):读取文件内容,并返回一个String;

writeStringToFile(File file,String content):将内容content写入到file中;

copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制

copyFile(File srcFile,File destFile);文件复制

  代码演示:

/*
 * 完成文件的复制
 */
public class CommonsIODemo01 {
    public static void main(String[] args) throws IOException {
        //method1("D:\test.avi", "D:\copy.avi");
        
        //通过Commons-IO完成了文件复制的功能
        FileUtils.copyFile(new File("D:\test.avi"), new File("D:\copy.avi"));
    }

    //文件的复制
    private static void method1(String src, String dest) throws IOException {
        //1,指定数据源 
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
        //2,指定目的地
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        //3,读
        byte[] buffer = new byte[1024];
        int len = -1;
        while ( (len = in.read(buffer)) != -1) {
            //4,写
            out.write(buffer, 0, len);
        }
        //5,关闭流
        in.close();
        out.close();
    }
}

/*
 * 完成文件、文件夹的复制
 */
public class CommonsIODemo02 {
    public static void main(String[] args) throws IOException {
        //通过Commons-IO完成了文件复制的功能
        FileUtils.copyFile(new File("D:\test.avi"), new File("D:\copy.avi"));
        
        //通过Commons-IO完成了文件夹复制的功能
        //D:基础班 复制到 C:\abc文件夹下
        FileUtils.copyDirectoryToDirectory(new File("D:\基础班"), new File("C:\abc"));
    }
}