Character Streams

Character Streams

[url]http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html[/url]


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();
}
}
}
}



[b]while ((c = inputStream.read()) != -1) {[/b]

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.

注意加粗的那一部分。CopyBytes和CopyCharacters都用了这个int变量。但是在CopyCharacters里,这个变量字符的值 16 bit 位 也就是 2个 byte ,在CopyBytes里,这个int变量保存了1 byte值,在它的末8位,看来还是有区别的。。。。 :shock:

[b]Character streams are often "wrappers" for byte streams. [/b]

FileReader, for example, uses FileInputStream, while FileWriter uses FileOutputStream.
There are two general-purpose byte-to-character "bridge" streams: [b]InputStreamReader and OutputStreamWriter[/b]. [b]Use them to create character streams when there are no prepackaged character stream classes that meet your needs.[/b] The [b][url=http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html]sockets lesson[/url][/b] in the networking trail shows [b]how to create character streams from the byte streams provided by socket classes[/b].

InputStreamReader and OutputStreamWriter 就干这事。转包byte stream--> character stream 。sockets 那个url里有演示

[url]http://java.sun.com/docs/books/tutorial/networking/index.html[/url]

[b]Line-Oriented I/O[/b]

面向行的IO ??

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").

行 \r\n \r \n 3种结束符号 。支持多种结束符。有利于多个平台使用

[b]BufferedReader and PrintWriter. [/b] 这2个类有面向行的接口。。


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.[b]readLine[/b]()) != null) {
outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}



[b]outputStream.println(l);[/b]

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 [b]might not be the same line terminator that was used in the input file. [/b]

这段有点意思:注意调用readLine 返回读取文件中的一行。但是输出使用println会加入一个换行符,这个输出的换行符是有操作系统决定的,很可能与文件中这一行的终结符是不一样的。 :oops:

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

除了字符和行这2个操作结构文档的方法,还要很多方法。see Scanning and Formatting.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值