Java字元流與位元組流區別與用法分析_java

來源:互聯網
上載者:User

本文執行個體講述了Java字元流與位元組流區別與用法。分享給大家供大家參考,具體如下:

位元組流與字元流主要的區別是他們的的處理方式

流分類:

1.Java的位元組流

InputStream是所有位元組輸入資料流的祖先,而OutputStream是所有位元組輸出資料流的祖先。

2.Java的字元流

Reader是所有讀取字串輸入資料流的祖先,而writer是所有輸出字串的祖先。

InputStream,OutputStream,Reader,writer都是抽象類別。所以不能直接new
位元組流是最基本的,所有的InputStream和OutputStream的子類都是,主要用在處理位元據,它是按位元組來處理的
但實際中很多的資料是文本,又提出了字元流的概念,它是按虛擬機器的encode來處理,也就是要進行字元集的轉化
這兩個之間通過 InputStreamReader,OutputStreamWriter來關聯,實際上是通過byte[]和String來關聯

在實際開發中出現的漢字問題實際上都是在字元流和位元組流之間轉化不統一而造成的

在從位元組流轉化為字元流時,實際上就是byte[]轉化為String時,

public String(byte bytes[], String charsetName)

有一個關鍵的參數字元集編碼,通常我們都省略了,那系統就用作業系統的lang
而在字元流轉化為位元組流時,實際上是String轉化為byte[]時,
byte[]    String.getBytes(String charsetName)
也是一樣的道理
至於java.io中還出現了許多其他的流,按主要是為了提高效能和使用方便,

如:BufferedInputStream,PipedInputStream等

具體實現

位元組流 FileInputStream FileOutputStream
字元流 FileReader FileWriter

字元流處理的單元為2個位元組的Unicode字元,分別操作字元、字元數組或字串,而位元組流處理單元為1個位元組,操作位元組和位元組數組。所以字元流是由Java虛擬機器將位元組轉化為2個位元組的Unicode字元為單位的字元而成的,所以它對多國語言支援性比較好!如果是音頻檔案、圖片、歌曲,就用位元組流好點,如果是關係到中文(文本)的,用字元流好點.

所有檔案的儲存是都是位元組(byte)的儲存,在磁碟上保留的並不是檔案的字元而是先把字元編碼成位元組,再儲存這些位元組到磁碟。在讀取檔案(特別是文字檔)時,也是一個位元組一個位元組地讀取以形成位元組序列.

位元組流可用於任何類型的對象,包括二進位對象,而字元流只能處理字元或者字串; 2. 位元組流提供了處理任何類型的IO操作的功能,但它不能直接處理Unicode字元,而字元流就可以。

位元組流轉換成字元流可以用 InputSteamReader OutputStreamWriter
轉換成BufferdReader BufferedWriter 他們具有緩衝區
例如:讀取檔案 從位元組流輸入到字元流輸入
定義一個位元組流:

FileInputStream fileInputStream = new FileInputStream("d:/text.txt");// 定義一個指向D:/TEXT.TXT 的位元組流InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);//位元組流轉換成InputStreamReaderBufferedReader bufferedReader = new BufferedReader(inputSteamReader);//InputStreamReader 轉換成帶緩衝的bufferedReader

可以把讀出來的內容賦值給字元

String ss = new String();String s;while((s = bufferedReader.readLine())!=null){    ss += s;}FileInputStream fileInputStream = new FileInputStream("d:/text.txt"); // 定義一個指向D:/TEXT.TXT 的位元組流   InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);  //位元組流轉換成InputStreamReader   BufferedReader bufferedReader = new BufferedReader(inputSteamReader);  //InputStreamReader 轉換成帶緩衝的bufferedReader  可以把讀出來的內容賦值給字元  String ss = new String();  String s;  while((s = bufferedReader.readLine())!=null){ ss += s; }

例如:寫入檔案 從位元組流輸出到字元流輸出

FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt");//定義一個指向D:/TEXT.TXT檔案OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(s);bufferedWriter.close();outputStreamWriter.close();fileOutputStream.close();FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt");  //定義一個指向D:/TEXT.TXT檔案OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);bufferedWriter.write(s);bufferedWriter.close();outputStreamWriter.close();fileOutputStream.close();

常式:

將字串轉化為位元組流 #region  將字串轉化為位元組流

/**/ ///  <summary>/// 將字串轉化為位元組流///  </summary>///  <param name="_Source"> 字串 </param>///  <returns> 位元組流 </returns>public  static  byte [] String2Bytes( string strSource){   System.IO.MemoryStream  memoryStream = new  System.IO.MemoryStream();   System.IO.BinaryWriter  binaryWriter = new  System.IO.BinaryWriter(memoryStream);   binaryWriter.Write( strSource );   byte []  buffer = memoryStream.GetBuffer();   return buffer;}#endregion

將位元組流轉化為字串 #region  將位元組流轉化為字串

/**/ ///  <summary>/// 將位元組流轉化為字串///  </summary>///  <param name="bytData"> 位元組流 </param>///  <returns> 字串 </returns>public  static  string Bytes2String( byte [] bytData){   // 位元組流->字串   System.IO.MemoryStream  memoryStream2 =  new  System.IO.MemoryStream(bytData);   System.IO.BinaryReader  binaryReader =  new  System.IO.BinaryReader(memoryStream2);   string  s2 = binaryReader.ReadString();   return s2;}#endregion

希望本文所述對大家java程式設計有所協助。

聯繫我們

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