java IO流 之 字元流,javaio流字元

來源:互聯網
上載者:User

java IO流 之 字元流,javaio流字元

字元流 :讀的也是二進位檔案,他會幫我們解碼成我們看的懂的字元。
字元流 = 位元組流  + 解碼

(一)字元輸入資料流:Reader : 它是字元輸入資料流的根類 ,是抽象類別

  FileReader :檔案字元輸入資料流 ,讀取字串。
     用法:
        1.找到目標檔案
        2.建立資料的通道
        3.建立一個緩衝區
        4.讀取資料
        5.關閉資源。

(二)字元流輸出資料流: Writer : 字元輸出資料流的根類 ,抽象的類
          FileWiter :檔案資料的輸出字元流
          使用注意點:
         1.FileReader內部維護了一個1024個字元的數組,所以在寫入資料的時候,它是現將資料寫入到內部的字元數組中。

      如果需要將資料寫入到硬碟中,需要用到flush()或者關閉或者字元數組資料存滿了。
         2.如果我需要向檔案中追加資料,需要使用new FileWriter(File , boolean)構造方法 ,第二參數true
         3.如果指定的檔案不存在,也會自己建立一個。

字元輸出資料流簡單案例

 1 import java.io.File; 2 import java.io.FileWriter; 3 import java.io.IOException; 4  5 public class fileWriter { 6  7     /** 8      * @param args 9      * @throws IOException 10      */11     public static void main(String[] args) throws IOException {12         // TODO Auto-generated method stub13         testFileWriter();14 15     }16     17     public static void testFileWriter() throws IOException{18         19         //1.找目標檔案20         File file = new File("D:\\a.txt");21         //2.建立通道22         FileWriter fwt = new FileWriter(file,true); //在檔案後面繼續追加資料23         //3.寫入資料(直接寫入字元)24         fwt.write("繼續講課");25         //4.關閉資料26         fwt.close();27     }28 }

 

字元輸入資料流簡單案例

 1 import java.io.File; 2 import java.io.FileReader; 3 import java.io.IOException; 4  5 public class fileReader { 6  7     public static void main(String[] args) throws IOException { 8  9         //testFileReader();10         testFileReader2();11     }12     //(1)輸入字元流的使用  這種方式效率太低。13     public static void testFileReader() throws IOException{14         15         //1.找目標檔案16         File file = new File("D:\\a.txt");17 18         //2.建立通道19         FileReader frd = new FileReader(file);20 21         //3.讀取資料22         int content = 0; //讀取單個字元。效率低23         while ((content = frd.read()) != -1) {24             System.out.print((char)content);    25         }26         27         //4.關閉流28         frd.close();29     }30     31     //(2)32     public static void testFileReader2() throws IOException{33         34         //1.找目標檔案35         File file = new File("D:\\a.txt");36 37         //2.建立通道38         FileReader frd = new FileReader(file);39 40         //3.建立一個緩衝區 ,字元數組41         char[] c = new char[1024];42         int length = 0;43 44         //4.讀取資料45         while ((length = frd.read(c))!= -1) {46             //字串輸出47             System.out.println(new String(c,0,length));48         }49         //5.關閉資源50         frd.close();51     }52 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.