day25 properties

1.properties將數組和持久存儲結合起來

  • 本身可當做這鍵值對存儲
public static void text1() {
        Properties pr = new Properties();
                pr.setProperty("a", "13");
        pr.setProperty("b", "43");
        pr.setProperty("c", "143");
        for (String a : pr.stringPropertyNames()) {
            System.out.println(a + "   " + pr.getProperty(a));
        }
建議使用特有setProperty和getProperty
stringPropertyNames返回一個包含key的set[]
    }
  • load()參數可以字節,字符輸入流,將讀取流對象中文件保存到集合
name=zhangsan
age=14
#height=178
此為a.txt文件,注意不要有空格,有空格會將空格視為key一部分
#表示注釋,不會被讀取到數組中
---------------------------------
Properties pr = new Properties();
        FileReader fReader = new FileReader("C:\\Userso\\Desktop\\a.txt");
        FileWriter fWriter = new FileWriter("C:\\Users\\Desktop\\b.txt");
        pr.load(fReader);
        pr.store(fWriter, "");
后面String不可以寫漢字,是文件注釋,通常為“”
可以不寫flush,store有此功能
        fWriter.close();
        fReader.close();

2.對象序列反序列

  • ObjectOutputStream對象序列,將對象序列化化到文件
  • ObjectInputStream對象序列,將對象反序列化化到程序
  • 因為是和對象相關,靜態成員不會被寫入
person類
public class Person implements Serializable{
// implements Serializable是一個標簽接口,沒有重寫方法,表示可以序列化
private String name;
private transient int age;
private static final long serialVersionUID =123456222;
// static final long serialVersionUID 需要原分不動前面修飾可加可不加,后面數字隨便寫,
這一句作用是避免在對象寫入文件后修改源碼不被識別錯誤,
在生成class文件后會有自動的標識碼一起寫入文件,
修改源碼就會改變標識碼,產生不匹配錯誤
由此句編譯器會計算class文件序列號
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 Person () {
    
}
public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}
@Override
public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
}
}
----------------------------------------
main文件
    public static void main(String[] args) throws IOException,ClassNotFoundException {
//      write();
        read();
    }

    public static void read() throws IOException, ClassNotFoundException {
        FileInputStream fi = new FileInputStream(
                "C:\\Users\\Desktop\\b.txt");
        ObjectInputStream oi = new ObjectInputStream(fi);
        Person sonPerson=(Person)oi.readObject();
        fi.close();
        oi.close();
        System.out.println(sonPerson);
    }

    public static void write() throws IOException {
        FileOutputStream fo = new FileOutputStream(
                "C:\\Users\\Desktop\\b.txt");
        ObjectOutputStream os = new ObjectOutputStream(fo);
        os.writeObject(new Person("zhangsan", 13));
        fo.close();
        os.close();
    }

3.打印流

  • printstream,printwriter(用較多)
  • 不負責數據源,僅負責輸出
  • 永遠不會拋出io異常但會有其他異常
  • 構造參數均有file,outputstream,字符串文件名 ,但printwriter多一個writer類
PrintWriter printWriter = new PrintWriter(new FileOutputStream(
                "C:\\Users\\fengbo\\Desktop\\b.txt"),true);
要自動刷新:是輸出流對象,true
        printWriter.println(200);
//      printWriter.close();

  • print打印僅有char[]一個數組,原因是字符串本身就是char數組,這是為字符串打印服務

4.第三方commons-io

  • 建floder,將class.jar文件復制,并且addpathbuild
  • 類filenameutiles,getextension返回擴展名,getname獲取文件名
  • 類fileutiles,readfiletoString讀文本返回字符串;writefiletoString讀文本返回字符串文本內容;
    writestringtofile將字符串寫回文本;
    copefile復制文件
    copedirectorytodirectory,復制文件夾
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容