java 字符流
Character Streams
The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.
---Java平台存储使用Unicode。字符流I / O自动翻译这种内部格式,并从本地字符集。在西方的语言环境中,本地字符集通常是----一个8位的ASCII的超集。
For most applications, I/O with character streams is no more complicated than I/O with byte streams. Input and output done with stream classes automatically translates to and from the local character set. A program that uses character streams in place of byte streams automatically adapts to the local character set and is ready for internationalization — all without extra effort by the programmer.
对于大多数应用, I / O字符流比i/o字节流简单。输入和输出流类,自动转换和从本地字符集。使用字符流字节流的一个程序,可以自动适应为本地字符集。
Using Character Streams
All character stream classes are descended from Reader
and Writer
. As with byte streams, there are character stream classes that specialize in file I/O: FileReader
and FileWriter
. The CopyCharacters
example illustrates these classes.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}
CopyCharacters
is very similar to CopyBytes
. The most important difference is that CopyCharacters
uses FileReader
andFileWriter
for input and output in place of FileInputStream
and FileOutputStream
. Notice that both CopyBytes
and CopyCharacters
use an int
variable to read to and write from. However, in CopyCharacters
, the int
variable holds a character value in its last 16 bits; in CopyBytes
, the int
variable holds a byte
value in its last 8 bits.
--- 在CopyCharacters , int变量保持在其最后的16位字符值;在CopyBytes , int变量保存在最后8位字节值。
Character Streams that Use Byte Streams
Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. FileReader
, for example, uses FileInputStream
, while FileWriter
uses FileOutputStream
.
--将夜-字符流使用字节流执行物理i/o当字符流在字符和字节进行转换时。例如,使用的FileReader ,文件输入流,而FileWriter中使用文件输出流。
here are two general-purpose byte-to-character "bridge" streams: InputStreamReader
and OutputStreamWriter
. Use them to create character streams when there are no prepackaged character stream classes that meet your needs
---有两种通用的字节到字符的桥梁:InputStreamReader 和OutputStreamWriter。当没有事先包装好的字符流类时使用它们创造字符流。
[java] view plaincopy
- FileInputStream fis;
- try {
- fis = new FileInputStream("E:/f.txt");
- InputStreamReader isr=new InputStreamReader(fis);
- BufferedReader br=new BufferedReader(isr);
- String line=null;
- while((line=br.readLine())!=null){
- System.out.println(line);
- }
Line-Oriented I/O
Character I/O usually occurs in bigger units than single characters. One common unit is the line: a string of characters with a line terminator at the end. A line terminator can be a carriage-return/line-feed sequence ("\r\n"
), a single carriage-return ("\r"
), or a single line-feed ("\n"
). Supporting all possible line terminators allows programs to read text files created on any of the widely used operating systems.
---遇到“\n”单个换行符 和 "\r"单个回车键 代表一行的结束
[java] view plaincopy
- public class CopyLines {
- public static void main(String[] args) throws IOException {
- BufferedReader inputStream = null;
- BufferedWriter outputStream = null;
- try {
- inputStream = new BufferedReader(new FileReader("e:/f.txt"));
- outputStream = new BufferedWriter(new FileWriter("e:/b.txt"));
- String l;
- while ((l = inputStream.readLine()) != null) {
- outputStream.write(l);
- }
- } finally {
- if (inputStream != null) {
- inputStream.close();
- }
- if (outputStream != null) {
- outputStream.close();
- }
- }
- }
- }
Invoking readLine
returns a line of text with the line.--调用返回文本的每一行