BufferedInputStream示例
BufferedOutputStream示例
BufferedReader實例
BufferedWriter示例
ByteArrayInputStream示例
Java ByteArrayOutputStream示例
Java CharArrayReader示例
Java CharArrayWriter示例
Java Console類示例
Java DataInputStream類型
Java DataOutputStream實例
Java Externalizable實例
Java File實例
FileDescriptor示例
Java FileInputStream示例
FileOutputStream示例
Java FilePermission示例
Java FileReader示例
Java FileWriter示例
Java FilterInputStream示例
Java FilterOutputStream示例
Java FilterReader示例
Java FilterWriter示例
Java Reader/Writer
Java讀取器和寫入器(Reader/Writer
)是基於字符的流。當要從數據源讀取基於字符的數據時,使用讀取器(Reader
)。當想要寫入基於字符的數據時使用寫入器(Writer
)。
如果有一個流提供字節,想通過將這些字節解碼爲字符讀取字符,應該使用InputStreamReader
類。
例如,如果有一個名爲iso
的InputStream
對象,並且想要獲取一個Reader
對象實例,那麼可以這樣做:
Reader reader = new InputStreamReader(iso);
如果知道在基於字節的流中使用的編碼,可在創建Reader
對象時指定它,如下所示:
Reader reader = new InputStreamReader(iso, "US-ASCII");
類似地,可以創建一個Writer
對象,從基於字節的輸出流中輸出的字符,如下所示,假設oso
是一個OutputStream
對象。以下代碼使從OutputStream
使用平臺默認編碼創建Writer
對象。
Writer writer = new OutputStreamWriter(oso);
使用「US-ASCII
」編碼從OutputStream
創建Writer
對象
Writer writer = new OutputStreamWriter(oso, "US-ASCII");
示例 -
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
String destFile = "test.txt";
try (BufferedWriter bw = new BufferedWriter(new FileWriter(destFile))) {
bw.append("test");
bw.newLine();
bw.append("test1");
bw.newLine();
bw.append("test2");
bw.newLine();
bw.append("test3");
bw.flush();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
以下代碼從test.txt
文件中讀取文本。
import java.io.BufferedReader;
import java.io.FileReader;
public class Main {
public static void main(String[] args) throws Exception{
String srcFile = "test.txt";
BufferedReader br = new BufferedReader(new FileReader(srcFile));
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
br.close();
}
}
將基於字節的流轉換爲基於字符的流是直接的。如果有一個InputStream
對象,可以通過將它包裝在一個InputStreamReader
對象中來獲得一個Reader
對象,如下所示:
InputStream is = create your InputStream object here;
Reader reader = new InputStreamReader(is);
要從InputStream
對象構造BufferedReader
對象,那麼可以這樣做:
InputStream is = create your InputStream object here;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
可以從OutputStream
對象構造一個Writer
對象,如下所示:
OutputStream os = create your OutputStream object here; Writer writer = new OutputStreamWriter(os);
示例
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.FilterReader;
import java.io.IOException;
class LowerCaseReader extends FilterReader {
public LowerCaseReader(Reader in) {
super(in);
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int count = super.read(cbuf, off, len);
if (count != -1) {
// Convert all read characters to lowercase
int limit = off + count;
for (int i = off; i < limit; i++) {
cbuf[i] = Character.toLowerCase(cbuf[i]);
}
}
return count;
}
}
public class Main {
public static void main(String[] args) throws Exception {
String fileName = "test.txt";
LowerCaseReader lcr = new LowerCaseReader(new FileReader(fileName));
int c = -1;
while ((c = lcr.read()) != -1) {
System.out.print((char) c);
}
lcr.close();
BufferedReader br = new BufferedReader(new LowerCaseReader(new FileReader(fileName)));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
}
執行上面代碼,得到以下結果 -
Yiibai.com BufferedInputStream Usge.
The Line 2..yiibai.com bufferedinputstream usge.
the line 2..