天天看點

JavaIO 總結筆記<五> 列印流、Properties類、對象流、序列化

個人部落格、個人簡書、gitHub首頁

一、列印流

1.我們以前在寫資料時,不管用什麼流,都需要調用什麼方法?

write(int )
write(byte[])
write(char[])
write(String)
           

我想直接向檔案中寫入int資料、char資料、boolean資料、小數,怎麼實作?

我們之前學的write()方法在寫資料時,對資料的類型是有限制的,一般都是先轉換為String,然後就可以寫入了

為了解決這個問題,Java專門提供了列印流,提供了一些獨有的方法,可以直接輸出(寫)各種類型的資料

2.類型

PrintStream 位元組列印流類 繼承自OutputStream

PrintWriter 字元列印流類 繼承自Writer

3.PrintStream類

  • 構造方法
public PrintStream(File file)
public PrintStream(String fileName)
           
  • 功能方法
print()
println()
           

4.PrintWriter類

  • 構造方法
public PrintWriter(File file)
public PrintWriter(String fileName)
           
  • 功能方法
public void print(Xxx  x)
public void println(Xxx  x)
           

代碼舉例:

/**
 * Created by AFinalStone on 2017/6/29.
 */
public class PrintDemo {

    public static String fileName_destination = IO7_PrintDemo;

    public static void writeByPrintStream() throws IOException {
        int score = ;
        double price = ;

        //示範PrintStream類
        //1.建管道
        PrintStream ps = new PrintStream(fileName_destination);
        //2.寫資料
        ps.println(score);
        ps.println(price);
        ps.println("writeByPrintStream");

        //3.關閉
        ps.close();
    }

    public static void writeByPrintWriter() throws IOException {
        //1.建管道
        PrintWriter pw = new PrintWriter(fileName_destination);

        //2.寫資料
        pw.println();
        pw.println();
        pw.println("writeByPrintWriter");

        //3.關閉
        pw.close();
    }
}
           

二、标準輸入輸出流

JavaIO 總結筆記<五> 列印流、Properties類、對象流、序列化

三、Properties類

1.擴充名是.properties的檔案叫屬性配置檔案,它也是文本檔案的一種

2.特點:一行一個鍵值對

3.Properties = HashMap + IO

4.構造方法 無參

5.功能方法

public Object setProperty(String key,String value) //put()
public String getProperty(String key)   //get()
public Set<String> stringPropertyNames()  //keySet()

public void list(PrintWriter out)  //把鍵值對放到輸出流中(寫)
public void store(Writer writer, String comments) //多了個備注
public void load(Reader reader)    //把屬性檔案中的鍵值對放到輸入流中(讀)
           

代碼舉例:

/**
 * Created by AFinalStone on 2017/6/29.
 */
public class PropertiesDemo {

    public static void writeProperties01() throws IOException {
        //示範Properties類   Properties = HashMap  +  IO

        //1.建立對象
        Properties prop=new Properties();

        //2.往屬性配置檔案中寫鍵值對
        prop.setProperty("name", "AFinalStone");
        prop.setProperty("pwd", "12345");
        prop.setProperty("email", "[email protected]");

        //3.把鍵值對寫到檔案中    list
        PrintWriter pw=new PrintWriter("demo.properties");
        prop.list(pw);

        //4.關閉
        pw.close();

    }

    public static void writeProperties02() throws IOException {
        //示範Properties類   Properties = HashMap  +  IO

        //1.建立對象
        Properties prop=new Properties();

        //2.往屬性配置檔案中寫鍵值對
        prop.setProperty("name", "AFinalStone");
        prop.setProperty("pwd", "12345");
        prop.setProperty("email", "[email protected]");

        //3.把鍵值對寫到檔案中    store
        FileWriter fw = new FileWriter("demo.properties");
        prop.store(fw, "--這是屬性配置檔案--");

        //4.關閉
        fw.close();
    }

    public static void readProperties() throws IOException {
        //示範Properties類的load方法

        //1.建立對象
        Properties prop=new Properties();

        //2.讀取資料
        FileReader fr=new FileReader("demo.properties");
        prop.load(fr);

        //3.關閉
        fr.close();

        //4.取資料
        Set<String> keys=prop.stringPropertyNames();    //keySet()
        for (String s : keys) {
            String val=prop.getProperty(s);     //get()
            System.out.println(s+"="+val);
        }
    }

}
           

四、對象流

1.簡述

  • 使用列印流寫一個對象時,寫進去的其實就是toString方法的傳回值
  • 序列化 把一個對象寫到檔案中
  • 反序列化 從一個檔案中把對象讀取出來
  • ObjectInputStream ObjectOutputStream
  • 在Java中,一個普通類的對象是預設不允許序列化的,如果像讓這個類的對象可以序列化,就要求這個類必須實作java.io.Serializable接口,這是第一次見到空接口,起到标記作用

2.ObjectOutputStream類

  • 作用
  • 構造方法 帶有一個參數
  • 功能方法

    writeObject(Object)

3.ObjectInputStream類

  • 作用
  • 構造方法 帶有一個參數
  • 功能方法

    readObject()

public class ObjectDemo {

    private static String fileName_Object = "student";


    /**
     * @throws ClassNotFoundException
     * 把對象轉換成檔案
     */
    public static void ObjectToFileByObjectInputStream() throws IOException, ClassNotFoundException {

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(fileName_Object));
        objectOutputStream.writeObject(new Student("gg", ));
        objectOutputStream.writeObject(new Student("tt", ));
        objectOutputStream.writeObject(new Student("rr", ));
        objectOutputStream.close();
    }


    /**
     * @throws ClassNotFoundException
     * 讀取檔案裡面的Object對象,
     */
    public static void FileToObjectByObjectInputStream() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileName_Object));

        for (int i = ; i < ; i++) {
            Object object = objectInputStream.readObject();
            System.out.println(object);
        }
        objectInputStream.close();
    }


}
           

九、序列化号 版本号

1.寫過對象後,如果改了類的源碼,那麼反序列化時會出異常

2.JVM怎麼知道我改了源碼了?類中預設自動有一個隐藏的屬性serialVersionUID,每次修改代碼,該屬性的值會自動修改

3.把serialVersionUID屬性人工的改為final即可

可以參考這篇文章:

序列化與反序列化的了解

項目位址:傳送門