學習java第18天個人總結

來源:互聯網
上載者:User

標籤:not   block   style   位元組   傳遞   padding   todo   讀寫   位元組流   

Day18個人總結

一、物件流程

作用:

1、讓對象在不同網路上進行傳遞

2、將對象存入檔案,需要講對象序列化,所謂的序列化就是將對象轉化為位元組的過程

物件流程:就是將對象序列化以後的位元組儲存到本地

具體實現過程分為以下幾步:

1、建立要儲存的類(類中屬性加上transient修飾符值將不會被寫入檔案)

2、使建立的類實現序列化介面

    這裡需要注意的是類在實現序列化介面(implements Serializable)會有警示,是因為有給這個類發布版本資訊發布版本資訊如下:

 

3、使用這個類建立對象並且賦值

4、建立物件流程

5、將對寫入檔案

物件流程的讀寫:

物件流程的特點是需要先寫後讀取,多個物件流程的讀取時按照儲存順序來讀取的,並且不能跳過任何一個對象。

物件流程寫入和讀取代碼如下:(關鍵代碼)

ObjectOutputStream oos1 = null;

       //建立person對象

       Person person1 = new Person("kobe",18);

       Person person2 = new Person("TD",19);

       ObjectInputStream ois1 = null;

       try {

           fos1 = new FileOutputStream(new File("a.txt"));

           //建立對象以及要寫入的檔案

           oos1 = new ObjectOutputStream(fos1);

           //將對象序列化寫入到檔案中

           oos1.writeObject(person1);

           oos1.writeObject(person2);

          

           //建立要讀取檔案的對象

           ois1 = new ObjectInputStream(new FileInputStream(new File("a.txt")));

           //這個類本身就是person類型 所以可以向下轉型

           Person object1 = (Person)ois1.readObject();

           System.out.println(object1);

           Person object2 = (Person)ois1.readObject();

           System.out.println(object2);

       }

二、轉換流

作用以及使用目的:

轉換流是將位元組流和字元流相互轉換的一個過程,將位元組流轉換為字元流是為了提高執行效率,反之是為了增加字元流的使用範圍。

轉化流使用如下:(關鍵代碼)

try {

           //將讀取b.txt中的檔案並且轉換為utf-8格式 列印輸出到控制台

           isr1 = new InputStreamReader(new FileInputStream(new File("b.txt")),"utf-8");

           osw1 = new OutputStreamWriter(new FileOutputStream(new File("c.txt")), "gbk");

          

           char[] chars = new char[10];

           int num = 0;

           while((num = isr1.read(chars)) != -1){

              //轉換成utf-8之後列印到控制台

              //System.out.println(chars);

              //以GBk的格式寫入到c.txt

              osw1.write(chars, 0, num);

           }

       }

三、標準輸入輸出以及重新導向錯誤流

作用:

標準輸入資料流(System)是為了讀取控制台使用者輸入內容,標準輸出資料流可以將內容從控制台輸出,輸出資料流又叫列印流(PrintWriter),可以列印將內容列印到檔案,或者將內容列印到控制台,具體可以通過構造方法實現控制如下:

 

具體使用如下:(關鍵代碼)

try {

           // 可以將內容寫入到 檔案中

           PrintWriter writer = new PrintWriter(new File("a.txt"));

           writer.write("哈哈哈哈哈");

           writer.println("heheheh");

           writer.append("xixixi");

           writer.flush();

       } catch (FileNotFoundException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       // 可以將內容寫到控制台上

       PrintWriter writer2 = new PrintWriter(System.out);

       writer2.write("哈哈哈哈哈");

       writer2.println("heheheh");

       writer2.append("xixixi");

       writer2.flush();

    }

 

重新導向錯誤流:

重新導向錯誤流重定方向標準輸入輸出資料流到檔案,可以定向到任意檔案

使用如下:(關鍵代碼)

FileInputStream fis;

       try {

           fis = new FileInputStream("a.txt");

           System.setIn(fis);// 重新定向 標準輸入資料流

           //   重新導向需要放在  標準輸入資料流之前

           InputStream is = System.in;

           byte[] b = new byte[80];

           int count = is.read(b);

           System.out.println(new String(b,0,count));

           // 標準輸出資料流的重新導向

           PrintStream ps = new PrintStream("d.txt");

           System.setOut(ps);// 標準輸出資料流的重新導向

           PrintStream ps2 = System.out;

           ps2.print("陰天下雨了");

           ps2.flush();

           // 標準錯誤流的重新導向

           System.setErr(ps);

           PrintStream ps3 = System.err;

           ps3.print("過了會天晴了");

           ps3.flush();

       }

四、記憶體流

記憶體流作用:

運行在記憶體中的流執行效率高,跟其他流不同的是,記憶體流是以記憶體為參照物,寫入記憶體使用量:ByteArrayOutputStream對象,讀取檔案使用ByteArrayInputStream對象使用如下:

 

使用如下:(關鍵代碼)

ByteArrayOutputStream baos = new ByteArrayOutputStream();

       try {

           // 向記憶體中寫入內容

           baos.write("呵呵呵呵".getBytes());

           baos.flush();

           // toByteArray() 將流中的內容 轉成byte數組

           byte[] b= baos.toByteArray();

           // 建立 記憶體輸入資料流

           ByteArrayInputStream bais = new ByteArrayInputStream(b);

           byte[] b2  = new byte[50];

           int count = bais.read(b2);

           System.out.println(new String(b2,0,count));

       }

各個方法:

ByteArrayOutputStream

 

 

註:writeTo(OutputStream out)方法內建自動重新整理功能寫入讀取檔案更加方便使用如下:

FileInputStream fis = null;

       ByteArrayOutputStream baos = null;

       try {

            fis = new FileInputStream(new File("a.txt"));

           baos = new ByteArrayOutputStream();

          

           byte[] bs = new byte[10];

           int count = 0;

           while((count= fis.read(bs))!=-1){

              // 寫到記憶體

              baos.write(bs, 0, count);

              baos.flush();

           }

          

          

           // 將記憶體中資料寫到本地檔案中

           // writeTo 等同於  上個類中自己寫的 writeFile

           baos.writeTo(new FileOutputStream(new File("w.txt")));

       }

ByteArrayInputStream

 

 

 

五、裝飾著模式

概念:在原有基礎上增加新的功能而不改變原有的方法,增加功能,提高效率,比如子類繼承父類的方法,重寫父類方法時不改變父類的方法,在原有方法上則增加一些功能。

學習java第18天個人總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.