您好,登錄后才能下訂單哦!
InputStreamReader和OutputStreamWriter源碼分析
1. InputStreamReader 源碼(基于jdk1.7.40)
package java.io; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import sun.nio.cs.StreamDecoder; // 將“字節(jié)輸入流”轉(zhuǎn)換成“字符輸入流” public class InputStreamReader extends Reader { private final StreamDecoder sd; // 根據(jù)in創(chuàng)建InputStreamReader,使用默認(rèn)的編碼 public InputStreamReader(InputStream in) { super(in); try { sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object } catch (UnsupportedEncodingException e) { // The default encoding should always be available throw new Error(e); } } // 根據(jù)in創(chuàng)建InputStreamReader,使用編碼charsetName(編碼名) public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException { super(in); if (charsetName == null) throw new NullPointerException("charsetName"); sd = StreamDecoder.forInputStreamReader(in, this, charsetName); } // 根據(jù)in創(chuàng)建InputStreamReader,使用編碼cs public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); } // 根據(jù)in創(chuàng)建InputStreamReader,使用解碼器dec public InputStreamReader(InputStream in, CharsetDecoder dec) { super(in); if (dec == null) throw new NullPointerException("charset decoder"); sd = StreamDecoder.forInputStreamReader(in, this, dec); } // 獲取解碼器 public String getEncoding() { return sd.getEncoding(); } // 讀取并返回一個(gè)字符 public int read() throws IOException { return sd.read(); } // 將InputStreamReader中的數(shù)據(jù)寫入cbuf中,從cbuf的offset位置開始寫入,寫入長度是length public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); } // 能否從InputStreamReader中讀取數(shù)據(jù) public boolean ready() throws IOException { return sd.ready(); } // 關(guān)閉InputStreamReader public void close() throws IOException { sd.close(); } }
2. OutputStreamWriter 源碼(基于jdk1.7.40)
package java.io; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import sun.nio.cs.StreamEncoder; // 將“字節(jié)輸出流”轉(zhuǎn)換成“字符輸出流” public class OutputStreamWriter extends Writer { private final StreamEncoder se; // 根據(jù)out創(chuàng)建OutputStreamWriter,使用編碼charsetName(編碼名) public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException { super(out); if (charsetName == null) throw new NullPointerException("charsetName"); se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); } // 根據(jù)out創(chuàng)建OutputStreamWriter,使用默認(rèn)的編碼 public OutputStreamWriter(OutputStream out) { super(out); try { se = StreamEncoder.forOutputStreamWriter(out, this, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } } // 根據(jù)out創(chuàng)建OutputStreamWriter,使用編碼cs public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); se = StreamEncoder.forOutputStreamWriter(out, this, cs); } // 根據(jù)out創(chuàng)建OutputStreamWriter,使用編碼器enc public OutputStreamWriter(OutputStream out, CharsetEncoder enc) { super(out); if (enc == null) throw new NullPointerException("charset encoder"); se = StreamEncoder.forOutputStreamWriter(out, this, enc); }java io系列01之 "目錄" // 獲取編碼器enc public String getEncoding() { return se.getEncoding(); } // 刷新緩沖區(qū) void flushBuffer() throws IOException { se.flushBuffer(); } // 將單個(gè)字符寫入到OutputStreamWriter中 public void write(int c) throws IOException { se.write(c); } // 將字符數(shù)組cbuf從off開始的數(shù)據(jù)寫入到OutputStreamWriter中,寫入長度是len public void write(char cbuf[], int off, int len) throws IOException { se.write(cbuf, off, len); } // 將字符串str從off開始的數(shù)據(jù)寫入到OutputStreamWriter中,寫入長度是len public void write(String str, int off, int len) throws IOException { se.write(str, off, len); }java io系列01之 "目錄" // 刷新“輸出流” // 它與flushBuffer()的區(qū)別是:flushBuffer()只會(huì)刷新緩沖,而flush()是刷新流,flush()包括了flushBuffer。 public void flush() throws IOException { se.flush(); } // 關(guān)閉“輸出流” public void close() throws IOException { se.close(); } }
說明:
OutputStreamWriter 作用和原理都比較簡單。
作用就是將“字節(jié)輸出流”轉(zhuǎn)換成“字符輸出流”。它的原理是,我們創(chuàng)建“字符輸出流”對(duì)象時(shí),會(huì)指定“字節(jié)輸出流”以及“字符編碼”。
示例程序
InputStreamReader和OutputStreamWriter的使用示例,參考源碼(StreamConverter.java):
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter;; import java.io.InputStreamReader; import java.io.IOException; /** * InputStreamReader 和 OutputStreamWriter 測試程序 * * */ public class StreamConverter { private static final String FileName = "file.txt"; private static final String CharsetName = "utf-8"; //private static final String CharsetName = "gb2312"; public static void main(String[] args) { testWrite(); testRead(); } /** * OutputStreamWriter 演示函數(shù) * */ private static void testWrite() { try { // 創(chuàng)建文件“file.txt”對(duì)應(yīng)File對(duì)象 File file = new File(FileName); // 創(chuàng)建FileOutputStream對(duì)應(yīng)OutputStreamWriter:將字節(jié)流轉(zhuǎn)換為字符流,即寫入out的數(shù)據(jù)會(huì)自動(dòng)由字節(jié)轉(zhuǎn)換為字符。 OutputStreamWriter out1 = new OutputStreamWriter(new FileOutputStream(file), CharsetName); // 寫入10個(gè)漢字 out.write("字節(jié)流轉(zhuǎn)為字符流示例"); // 向“文件中”寫入"0123456789"+換行符 out1.write("0123456789\n"); out1.close(); } catch(IOException e) { e.printStackTrace(); } } /** * InputStreamReader 演示程序 */ private static void testRead() { try { // 方法1:新建FileInputStream對(duì)象 // 新建文件“file.txt”對(duì)應(yīng)File對(duì)象 File file = new File(FileName); InputStreamReader in = new InputStreamReader(new FileInputStream(file), CharsetName); // 測試read(),從中讀取一個(gè)字符 char c1 = (char)in1.read(); System.out.println("c1="+c1); // 測試skip(long byteCount),跳過4個(gè)字符 in1.skip(6); // 測試read(char[] cbuf, int off, int len) char[] buf = new char[10]; in1.read(buf, 0, buf.length); System.out.println("buf="+(new String(buf))); in.close(); } catch(IOException e) { e.printStackTrace(); } } }
運(yùn)行結(jié)果:
c1=字 buf=流示例0123456
結(jié)果說明:
(01) testWrite() 的作用是將“內(nèi)容寫入到輸出流”。寫入的時(shí)候,會(huì)將寫入的內(nèi)容轉(zhuǎn)換utf-8編碼并寫入。
(02) testRead() 的作用是將“內(nèi)容讀取到輸入流”。讀取的時(shí)候,會(huì)將內(nèi)容轉(zhuǎn)換成utf-8的內(nèi)容轉(zhuǎn)換成字節(jié)并讀出來。
生成的文件utf-8的file.txt的16進(jìn)制效果圖如下:
將StreamConverter.java中的CharsetName修改為"gb2312"。運(yùn)行程序,生產(chǎn)的file.txt的16進(jìn)制效果圖如下:
以上所述是小編給大家介紹的Java中的InputStreamReader和OutputStreamWriter源碼分析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。