2020.8.4

一、 今日学习内容

  1、对象序列化与反序列化

    (1)序列化

           所谓的对象序列化就是将一一个在内存中保存的对象变成一个二进制的数据流进行传输,但并不是所有类的对象都可以进行序列化操作,如果-一个对 象需要被序列             化,则对象所在的类必须实现Serializable接口。但此接口中没有任何的方法定义,是作为标识接口出现的。

      示例:将实体类标注为可以序列化

import java. io. Serializable;
public class Person implements Serializable{
         private String name;
         private int 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   "姓名: "+name+", 年龄: "+age;
          }
}

       通过上面的代码,此对象已经可以实现序列化,现在可以使用ObjectOutputStream将对象写到文件中去。
       示例:使用ObjectOutputStream 将对象写到文件中去

import java. io. FileOutputStream;
import java. io. IOException;
import java. io. ObjectOutputStream;
public class ObjectOutputstreamDemo {
       public static void main (String[] args) throws IOException {
            //创建对象
            ObjectOutputStream oos= new ObjectOutputStream(new
               FileOutputStream ("D:/Hello.txt"));
            //需要序列化的对象
               Person p =new Person() ;
               p. setAge (20);
               p. setName ("伍正云") ;
               //将对象写进文件中
               oos .writeobject(p) ;
               //关闭
               oos.close() ;
       }
}

         (2)反序列化
             反序列化是将文件中读取对象的过程。使用ObjeInputStream可以实现反序列化。
             示例:反序列化

import java.io.FileInputStream;
import java. io. IOException;
import java. io. ObjectInputStream;
public class ObjectInputStreamDemo {
      public static void main (String[] args) throws IOException,
      ClassNotFoundException {
          //创建对象
          ObjectInputStream ois = new ObjectInputStream(
          new FileInputStream ("D: /Hello.txt")) ;
          //从文件中读取对象
           Person p=(Person)ois.readobject();
          //关闭
          ois.close() ;
          System. out.println(p) ;
      }
}

        (3) transient 关键字
    实际上在进行对象序列化时,序列化的是类中的属性,因为每个类的对象只有属性是不同的,但是如果现在有某个属性不希望被序列化,则可以使用transient关键字。

public class Person implements Serializable {
    private transient String name;
    private int age;
 }

      (4)序列化一组对象
            通常在实际开发中肯定不只序列化一个对象,如果要想序列化多个对象,可以使用数组来解决。
           示例:序列化多个对象

import java. io.FileOutputstream;
import java. io. IOException;
import java.io. objectOutputStream;
public class objectoutputstreamDemo2 {
    public static void main(String[] args) throws IOException{
          //创建对象
        objectoutputStream oos = new ObjectOutputStream (
        new FileOutputStream ("D: /Hello. txt")) ;
        //需要序列化的对象
        Person p= new Person() ;
        p.setAge (20) ;
        p.setName ("伍正云"):
        Person p2 = new Person() ;
        p. setAge (20);
        p.setName ("伍正云22");
        Person[] ps={p,p2};
        //将对象写进文件中
        oos.writeobject (ps) ;
        //关闭
        oos.close() ;
    }
}

    2、打印流

         如果使用Outputream输出数据,不是很方便,所以,Java提供了打印流PrintStream以后只要遇到打印文件,首先考虑使用PrintStream。

         示例:使用PrintStream写入数据

import java. io.FileNotFoundExcept ion;
importj ava. io. FileOutputStream;
import java.io. PrintStream;
public class PrintStreamDemo{
    public static void main(String[] args) throws FileNotFoundException {
          PrintStream out = new Printstream (
          new FileOutputStream("D: /Hello.txt")) ;
          //打印布尔类型
          out.println (true) ;
         //打印字符类型
         out.println('A') ;
         //打印double类型
        out.println(10.0) ;
         //打印整型
        out.println(12) ;
         //打印字符串
        out.println("Hello") ;
         //打印对象
        Person p= new Person() ;
        p.setAge (20) ;
        p.setName ("sky");
        out.println(p) ;
        //关闭
        out.close() ;
   }
}

     3、RandomAccessFile随机访问文件

        RandomAccessFile可以随机读写文件,随机读写文件就是说可以任意访问文件的位置,这是其他流所不能操纵的。RandomAccessFile 类包含一个记录指针,用于标识当前流的读写位置,这个位置可以向前移动,也可以向后移动。RandomAccessFile包含两个方法来操作文件记录指针。
     #long getFilePoint():记录文件指针的当前位置。
     #void seek(long pos):将文件记录指针定位到pos位置。
    RandomAccessFile类的构造函数如下。

public,RandomAccessFile (File file,String mode)throws FileNotFoundException
public RandomAccessFile (String name,String mode)throws FileNotFoundException

       在RandomAccessFile中读写文件有四种模式如表所示。

     

       示例:使用RandomAccessFile随机读写文件

import java. io. IOException; 
import java. io. RandomAccessFile;
public class RandomAccessFileDemo
  public static void main (String[] args) throws IOException {
    //创建对象
    RandomAccessFile acf =new RandomAccessFile ("D: /Hello.txt", "rw");
    //写byte数组
     byte[] b = "Hello Java! !".getBytes() ;
     acf.write(b) ;
    //设置偏移量
   acf.seek(6) ;
   System. out. println("pointer="+acf .getFilePointer()) ;
   byte[] b2="C++" .getBytes() ;
   acf.write (b2) ;
   int len=-1;
   byte[] buf=new byte [1024] ;
   //将偏移量设置成第一个位置
   acf .seek(0) ;
   System. out. println ("pointer "+acf .getFilePointer());
   while( (len=acf . read (buf)) !=-1) {
       String s =new String (buf,0, len) ;
       System. out.println(s) ;
   }
 }
}

二、遇到的问题

    对随机读取文件偏移量不理解

三、明日计划

   完成《大道至简》的读后感

原文地址:https://www.cnblogs.com/wmdww/p/13432372.html