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.

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.

If internationalization isn’t a priority, you can simply use the character stream classes without paying much attention to character set issues. Later, if internationalization becomes a priority, your program can be adapted without extensive recoding. See the Internationalization trail for more information.

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 and FileWriter 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.

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.

There 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. The sockets lesson in the networking trail shows how to create character streams from the byte streams provided by socket classes.

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.

Let’s modify the CopyCharacters example to use line-oriented I/O. To do this, we have to use two classes we haven’t seen before, BufferedReader and PrintWriter. We’ll explore these classes in greater depth in Buffered I/O and Formatting. Right now, we’re just interested in their support for line-oriented I/O.

The CopyLines example invokes BufferedReader.readLine and PrintWriter.println to do input and output one line at a time.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class CopyLines {
public static void main(String[] args) throws IOException {

    BufferedReader inputStream = null;
    PrintWriter outputStream = null;

    try {
        inputStream = new BufferedReader(new FileReader("xanadu.txt"));
        outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));

        String l;
        while ((l = inputStream.readLine()) != null) {
            outputStream.println(l);
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

}
Invoking readLine returns a line of text with the line. CopyLines outputs each line using println, which appends the line terminator for the current operating system. This might not be the same line terminator that was used in the input file.

There are many ways to structure text input and output beyond characters and lines. For more information, see Scanning and Formatting.

译文:

字符流

JAVA平台使用Unidcode存储字符的值。字符流输入、输出自动的将这种内部的格式从本地字符集转换。在西部地区,本地字符集通常是ASCII字符集的超集。
对于大多数应用程序来说,字符流输入输出没有字节输入输出复杂。输入输出使用流的类自动从本地字符集转换。一个使用字符流而不用字节流自动的适配本地的字符集的程序已经是国际化了,这些都不用程序员额外去费神。
如果国际化不是优先考虑的,你可以简单的使用字符流类而不需要花费很多精力在字符集问题上。以后,如果国际化成为了优先考虑,你的程序可以适配,而不需要扩展的编码。你可以在“国际化跟踪”这一章得到更多的信息。
使用字符流
所有的字节流类都是来自于reader和writer。和字节流一样,在文件输入输出中也有很多字符流类。FlieReader和FileWriter。下面的“复制字符”的例子阐述了这个类。
这个例子很像上一章中的“复制字节”例子。这两个例子最重要的不同之处是本章的例子使用了FileReader和FileWriter来输入、输出而不是用FileInputStream和FileOutputStream。注意这两个例子都使用了int变量来读取和写入。然后,在复制字符的例子中,int变量保持了一个字符最后16位的值。而在复制字节的例子中,int变量保持了一个字节的最后8位的值。
使用字节流的字符流
字符流经常作为字节流的封装器。字符流使用了字节流来执行物理上的输入输出,当字符流执行字符和字节之间的翻译。举个例子,FileReader使用FileInputStream,而FileWriter使用FielOutputStream。
面向行的I/O
字符I/O通常出现在比单独的字符更大的单位里。一个常用的单位是行:一个结尾使用行结尾的字符串。一个行结尾符可以是返回值或者换行符(“\r\n”),一个单独的返回值(“\r”),或者一个单独的返回值(“\n”)。支持所有可能的行结尾符可以让程序读取被任何一个开放的系统广泛创建的文本文件。
让我们修改复制字符的例子,使用面向行的I/O。要做这件事,我们得使用两个之间没有见过的类,BufferedReader和PrintWriter。我们将会在“Buffered I/O和PrintWirter”这一章中更深的研究这些类。现在,我们只需要关注他们对面向行的I/O中的支持。
“复制行”例子调用了BufferedReader.readeline方法和PrintWriter.println方法来执行一次输入和输出一行。
调用readLine方法返回了一行文本。“复制行”使用了给最近的操作系统附上了行终止符操作的换行输出方法输出了每一行。这可能和在输入文件中使用的行终止符不相同。
除了字符和行,还有很多方法可以构造文本的输入和输出。想看到更多信息,可以看“浏览和格式化”这一章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值