Serialization and deserialization stream flows, the print flow Commons

Serialization stream:

For the operational flow to be written into the stream ObjectOutputStream

Deserialization:

For reading the object from the stream flow of operations ObjectInputStream

import java.io.Serializable;

public class Person implements Serializable{
  private  String name;
  private static 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;
  }
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }

}

public class Demo01 {
  public static void main(String[] args) throws IOException, ClassNotFoundException {
    //write();
    read();
  }
  //序列化
  public static void write() throws IOException {
    // 序列前提:要序列化的类必须实现Serializable接口
    //静态不能序列化
    // 目的地
    FileOutputStream fos = new FileOutputStream("D:\\java\\person.txt");
    // 创建对象
    Person p = new Person("zhangsan", 18);
    // 创建序列化流
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    // 将person对象写入文件中
    oos.writeObject(p);
    // 释放资源
    oos.close();
  }
  //反序列化
  public static void read() throws ClassNotFoundException, IOException{
    //数据源
    FileInputStream fis=new FileInputStream("D:\\java\\person.txt");
    //创建反序列化流
    ObjectInputStream ois=new ObjectInputStream(fis);
    //将文件中的对象读到person对象中
    Person p=(Person)ois.readObject();
    System.out.println(p);
    //释放资源
    ois.close();
  }
}

通过在流中使用文件可以实现对象的持久存储。

 

Serializable标记接口给需要序列化的类,提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。

 

public class Person implements Serializable{

  //给类显示声明一个序列版本号

  private static final long serialVersionUID = 1L;
  //瞬态关键字transient
  private  String name;
  private static 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;
  }
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }
}

瞬态关键字transient:

当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属性就不会序列化了。

 

静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。

public class Person implements Serializable{
  private static final long serialVersionUID = 1L;
  //瞬态关键字transient
  private transient String name;
  private static 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;
  }
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }
}

打印流:

打印流是输出信息最方便的类,注意包含字节打印流PrintStream和字符打印流:PrintWriter。打印流提供了非常方便的打印功能,

可以打印任何类型的数据信息,例如:小数,整数,字符串。

在这个类中定义了很多print()和println()方法,System.out.print()方法可以打印任何数据类型。

 

构造方法:

public PrintStream(OutputStream out)  --指定输出位置

此构造方法接收OutputStream的子类,

打印流好处:

通过定义的构造方法可以发现,有一个构造方法可以直接接收OutputStream类的实例,与OutputStream相比起来,PrintStream可以更方便的输出数据,

相当于把OutputStream类重新包装了一下,使之输出更方便。

public static void main(String[] args) throws FileNotFoundException {
  //目的地
  //开启续写功能
  FileOutputStream fos=new FileOutputStream("D:\\java\\a.txt",true);//开启文件自动刷新写入功能

  //开启自动刷新功能
  PrintWriter pw=new PrintWriter(fos,true);
  //向文件中写入字符串
  pw.print(100);
  pw.println("bbbb");
  pw.print("cccc");
  //释放资源
  pw.close();
}

 Commons工具类:

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

创建lib文件夹

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

 

 

常用方法:

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

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

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

右键点击commons-io.jarBuild PathAdd to Build Path

 

public static void main(String[] args) {
  //获取文件拓展名
  String ext=FilenameUtils.getExtension("D:\\java\\a.txt");
  System.out.println("拓展名为"+ext);
  //获取文件名
  String fname=FilenameUtils.getName("D:\\java\\a.txt");
  System.out.println("文件名为"+fname);
  //判断拓展名
  boolean flag=FilenameUtils.isExtension("D:\\java\\a.txt", "txt");
  System.out.println(flag);
}

运行结果:

 

FileUtils工具类:

提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法

常用方法:

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

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

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

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

public static void main(String[] args) throws IOException {
//write();
//copydir();
copys();
}
public static void read() throws IOException{
  File file=new File("D:\\java\\a.txt");
  String content=FileUtils.readFileToString(file);
  System.out.println(content);
  }
  public static void write() throws IOException{
    //目的地
    File file=new File("D:\\java\\a.txt");
    FileUtils.writeStringToFile(file, "你好吗",true);
  }
  //复制文件夹
  public static void copydir() throws IOException{
    //数据源
    File src=new File("D:\\java");
    //目的地
    File dest=new File("D:\\java2");
    //开始复制
    FileUtils.copyDirectoryToDirectory(src, dest);
  }
  //复制文件
  public static void copys() throws IOException{
    File file=new File("D:\\java\\b.txt");
    File f=new File("D:\\java\\bb.txt");
    FileUtils.copyFile(file, f);
  }

 

Guess you like

Origin www.cnblogs.com/boss-H/p/11056609.html