Java中的InputStreamReader和OutputStreamWriter源碼分析_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
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ù)寫(xiě)入cbuf中,從cbuf的offset位置開(kāi)始寫(xiě)入,寫(xiě)入長(zhǎng)度是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è)字符寫(xiě)入到OutputStreamWriter中
public void write(int c) throws IOException {
se.write(c);
}
// 將字符數(shù)組cbuf從off開(kāi)始的數(shù)據(jù)寫(xiě)入到OutputStreamWriter中,寫(xiě)入長(zhǎng)度是len
public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len);
}
// 將字符串str從off開(kāi)始的數(shù)據(jù)寫(xiě)入到OutputStreamWriter中,寫(xiě)入長(zhǎng)度是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();
}
}
說(shuō)明:
OutputStreamWriter 作用和原理都比較簡(jiǎn)單。
作用就是將“字節(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 測(cè)試程序
*
*
*/
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)換為字符流,即寫(xiě)入out的數(shù)據(jù)會(huì)自動(dòng)由字節(jié)轉(zhuǎn)換為字符。
OutputStreamWriter out1 = new OutputStreamWriter(new FileOutputStream(file), CharsetName);
// 寫(xiě)入10個(gè)漢字
out.write("字節(jié)流轉(zhuǎn)為字符流示例");
// 向“文件中”寫(xiě)入"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);
// 測(cè)試read(),從中讀取一個(gè)字符
char c1 = (char)in1.read();
System.out.println("c1="+c1);
// 測(cè)試skip(long byteCount),跳過(guò)4個(gè)字符
in1.skip(6);
// 測(cè)試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é)果說(shuō)明:
(01) testWrite() 的作用是將“內(nèi)容寫(xiě)入到輸出流”。寫(xiě)入的時(shí)候,會(huì)將寫(xiě)入的內(nèi)容轉(zhuǎn)換utf-8編碼并寫(xiě)入。
(02) testRead() 的作用是將“內(nèi)容讀取到輸入流”。讀取的時(shí)候,會(huì)將內(nèi)容轉(zhuǎn)換成utf-8的內(nèi)容轉(zhuǎn)換成字節(jié)并讀出來(lái)。
生成的文件utf-8的file.txt的16進(jìn)制效果圖如下:

將StreamConverter.java中的CharsetName修改為"gb2312"。運(yùn)行程序,生產(chǎn)的file.txt的16進(jìn)制效果圖如下:

以上所述是小編給大家介紹的Java中的InputStreamReader和OutputStreamWriter源碼分析,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot之跨域過(guò)濾器配置允許跨域訪問(wèn)方式
這篇文章主要介紹了SpringBoot之跨域過(guò)濾器配置允許跨域訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會(huì)轉(zhuǎn)發(fā)到?BasicErrorController?中進(jìn)行異常寫(xiě)入,然后才會(huì)返回客戶端。所以,我們可以在?BasicErrorController?對(duì)?filter異常進(jìn)行捕獲并處理,下面通過(guò)本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05
Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java Swing SpringLayout彈性布局的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
使用Spring的StopWatch實(shí)現(xiàn)代碼性能監(jiān)控的方法詳解
在開(kāi)發(fā)過(guò)程中,偶爾還是需要分析代碼的執(zhí)行時(shí)間,Spring 框架提供了一個(gè)方便的工具類 StopWatch,本文將介紹 StopWatch 的基本用法,并通過(guò)示例演示如何在項(xiàng)目中使用 StopWatch 進(jìn)行代碼性能監(jiān)控2023-12-12
淺談BeanPostProcessor加載次序及其對(duì)Bean造成的影響分析
這篇文章主要介紹了淺談BeanPostProcessor加載次序及其對(duì)Bean造成的影響分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Java Web端程序?qū)崿F(xiàn)文件下載的方法分享
這篇文章主要介紹了Java Web端程序?qū)崿F(xiàn)文件下載的方法分享,包括一個(gè)包含防盜鏈功能的專門針對(duì)圖片下載的程序代碼示例,需要的朋友可以參考下2016-05-05
基于SpringBoot+Redis的Session共享與單點(diǎn)登錄詳解
這篇文章主要介紹了基于SpringBoot+Redis的Session共享與單點(diǎn)登錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Java如何實(shí)現(xiàn)支付寶電腦支付基于servlet版本
這篇文章主要介紹了Java如何實(shí)現(xiàn)支付寶電腦支付基于servlet版本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

