8.5IO流對象學(xué)習(xí)2

子符輸出流緩沖流

BufferedWrite繼承Writer
方法 writer() 寫單個字符 字符數(shù)組 字符串
構(gòu)造方法:
BufferedReader(Reader r)
可以傳遞任意的字符輸出流
傳遞誰,就高效誰
能傳遞的字符輸出流 FileWriter, OutputStreamWriter

 BufferedWriter 具有自己特有的方法
   void  newLine() 寫換行
  
 newLine()文本中換行, \r\n也是文本換行
  方法具有平臺無關(guān)性
Windows  \r\n
 Linux    \n
  
newLine()運行結(jié)果,和操作系統(tǒng)是相互關(guān)系
  JVM: 安裝的是Windows版本,newLine()寫的就是\r\n
         安裝的是Linux版本,newLine()寫的就是\n

代碼

public class BufferedWrierDemo {
    public static void main(String[] args) throws IOException{
        //創(chuàng)建字符輸出流,封裝文件
        FileWriter fw = new FileWriter("c:\\buffer.txt");
        BufferedWriter bfw = new BufferedWriter(fw);
        
        bfw.write("你好");
        bfw.newLine();
        bfw.flush();
        
        
        bfw.write("我好好");
        bfw.newLine();
        bfw.flush();

        bfw.write("大家都好");
        bfw.flush();
        
        bfw.close();
        
    }
}

字符輸入緩沖流

讀取功能 read() 單個字符,字符數(shù)組
構(gòu)造方法:
BufferedReader(Reader r)
可以任意的字符輸入流
FileReader InputStreamReader

 BufferedReader自己的功能
  String readLine() 讀取文本行 \r\n

方法讀取到流末尾,返回null

readLine()方法返回行的有效字符,沒有\(zhòng)r\n

public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException {
        int lineNumber = 0;
        //創(chuàng)建字符輸入流緩沖流對象,構(gòu)造方法傳遞字符輸入流,包裝數(shù)據(jù)源文件
        BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt"));
        //調(diào)用緩沖流的方法 readLine()讀取文本行
        //循環(huán)讀取文本行, 結(jié)束條件 readLine()返回null
        String line = null;
        while((line = bfr.readLine())!=null){
            lineNumber++;
            System.out.println(lineNumber+"  "+line);
        }
        bfr.close();
    }
}

Properties類

  • 集合對象Properties類,繼承Hashtable,實現(xiàn)Map接口
    可以和IO對象結(jié)合使用,實現(xiàn)數(shù)據(jù)的持久存儲
Properties集合的特有方法

1、store(OutputStream out)
store(Writer w)
接收所有的字節(jié)或者字符的輸出流,將集合中的鍵值對,寫回文件中保存

public static void function()throws IOException{
        Properties pro = new Properties();
        pro.setProperty("name", "zhangsan");
        pro.setProperty("age", "31");
        pro.setProperty("email", "123456789@163.com");
        FileWriter fw = new FileWriter("c:\\pro.properties");
        //鍵值對,存回文件,使用集合的方法store傳遞字符輸出流
        pro.store(fw, "");
        fw.close();
    }

2、load(InputStream in)
load(Reader r)
傳遞任意的字節(jié)或者字符輸入流
流對象讀取文件中的鍵值對,保存到集合

public static void function_1()throws IOException{
        Properties pro = new Properties();
        FileReader fr = new FileReader("c:\\pro.properties");
        //調(diào)用集合的方法load,傳遞字符輸入流
        pro.load(fr);
        fr.close();
        System.out.println(pro);
    }

使用Properties集合,存儲鍵值對
* setProperty等同與Map接口中的put
* setProperty(String key, String value)
* 通過鍵獲取值, getProperty(String key)

    public static void function(){
        Properties pro = new Properties();
        pro.setProperty("a", "1");
        pro.setProperty("b", "2");
        pro.setProperty("c", "3");
        System.out.println(pro);
        
        String value = pro.getProperty("c");
        System.out.println(value);
        
        //方法stringPropertyNames,將集合中的鍵存儲到Set集合,類似于Map接口的方法keySet
        Set<String> set = pro.stringPropertyNames();
        for(String key : set){
            System.out.println(key+"..."+pro.getProperty(key));
        }
    }

對象的序列化和反序列化

IO流對象,實現(xiàn)對象Person序列化,和反序列化

  • ObjectOutputStream 寫對象,實現(xiàn)序列化
  • ObjectInputStream 讀取對象,實現(xiàn)反序列化
public class ObjectStreamDemo {
    public static void main(String[] args)throws IOException, ClassNotFoundException {
//      writeObject();
        readObject();
    }
    /*
     * ObjectInputStream
     * 構(gòu)造方法:ObjectInputStream(InputStream in)
     * 傳遞任意的字節(jié)輸入流,輸入流封裝文件,必須是序列化的文件
     * Object readObject()  讀取對象
     */
    public static void readObject() throws IOException, ClassNotFoundException{
        FileInputStream fis = new FileInputStream("c:\\person.txt");
        //創(chuàng)建反序列化流,構(gòu)造方法中,傳遞字節(jié)輸入流
        ObjectInputStream ois = new ObjectInputStream(fis);
        //調(diào)用反序列化流的方法 readObject()讀取對象
        Object obj =ois.readObject();
        System.out.println(obj);
        ois.close();
    }
    /*
     * ObjectOutputStream
     * 構(gòu)造方法: ObjectOutputStream(OutputSteam out)
     * 傳遞任意的字節(jié)輸出流
     * void writeObject(Object obj)寫出對象的方法
     */
    public static void writeObject() throws IOException{
        //創(chuàng)建字節(jié)輸出流,封裝文件
        FileOutputStream fos = new FileOutputStream("c:\\person.txt");
        //創(chuàng)建寫出對象的序列化流的對象,構(gòu)造方法傳遞字節(jié)輸出流
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Person p = new Person("lisi",25);
        //調(diào)用序列化流的方法writeObject,寫出對象
        oos.writeObject(p);
        oos.close();
    }
}

person類

public class Person implements Serializable{
    public String name;
    public int age;
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person(){}
    
    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 + "]";
    }

    
}

注:1、序列化必須實現(xiàn)serializable接口
serializable接口沒有抽象方法,起標(biāo)記作用
2、transinent瞬態(tài)關(guān)鍵字
阻止成員變量序列化
String StringBuilder StringBuffer

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容