java的character用法_【Java使用手册】-03 Character Streams

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.

译:字符流

Java 平台使用 Unicode 约定存储字符值。字符流 I/O 自动地将这种内部格式与本地字符集进行转换。在西方地区中,本地字符集通常是 ASCII 的8位超集。

对于大多数应用程序,字符流的 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.

译:使用字符流

所有字符流的类都派生自 Reader 和 Writer 。与字节流一样,在文件 I/O 也有专门的字符流的类: FileReader 和 FileWriter 。下面的示例 CopyCharacters 说明了这些类。

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.

译:

CopyCharacters 非常类似于 CopyBytes 。最重要的区别是 CopyCharacters 使用 FileReader 和FileWriter 来代替 FileInputStream 和 FileOutputStream 用作输入和输出。注意, CopyBytes 和CopyCharacters 都使用 int 类型的变量进行读写。然而,在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.

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.

译:使用字节流的字符流

字符流通常是字节流的“包装器”。字符流使用字节流进行物理上的 I/O,而字符流处理字符和字节之间的转换。例如,' FileReader '使用' FileInputStream ',而' FileWriter '使用' FileOutputStream '。

有两种通用的字节到字符的“桥”流: InputStreamReader 和OutputStreamWriter 。当没有能够满足你需要的预包装字符流类时,可以使用他们来创建字符流。networking trail中的sockets lesson展示了如何从套接字类提供的字节流中创建字符流。

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.

译:面向行的 I/O

字符 I/O 通常以比单个字符更大的单位出现。一个常见单位是行:以行结束符结尾的字符串。行结束符可以是一个回车/换行的序列 ("\r\n"),可以是一个单独的回车("\r"),或者一个单独的换行("\n")。支持所有可能的行终止符允许程序读取在任何广泛使用的操作系统上创建的文本文件。

我们来调整 CopyCharacters 示例,以使用面向行的 I/O。要做到这一点,我们必须使用两个以前没有见过的类, BufferedReader 和PrintWriter 。我们将在 Buffered I/O and Formatting 更深入的探讨这些类。现在,我们感兴趣的只是他们对面向行 I/O 的支持。

CopyLines 示例调用 BufferedReader.readLine 和PrintWriter.println 来完成每次输入或输出一行。

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.

译:

调用 readLine 返回一行文本。CopyLines 使用 println 输出每一行,并在行尾附加一个当前操作系统的行结束符。这可能和输入文件里的行结束符不是同一种。

除了字符和行,还有很多方法来组织文本输入和输出。想获取更多信息,请参加 Scanning and Formatting。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值