---
title: "Character Streams"
author: "wutao8818"
date: "2009-06-30T23:32:55Z"
tags: ["C", "C#", "C++", "SUN", "Socket"]
slug: "character-streams"
draft: false
lastmod: "2009-06-30T23:51:41Z"
id: 417909
url: "https://wutaoo.iteye.com/blog/417909"
---

Character Streams

<http://java.sun.com/docs/books/tutorial/essential/io/charstreams.html>


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

```


  **while ((c = inputStream.read()) != -1) {**

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: 

**Character streams are often "wrappers" for byte streams. **

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](http://java.sun.com/docs/books/tutorial/networking/sockets/readingWriting.html)** in the networking trail shows **how to create character streams from the byte streams provided by socket classes**.

 InputStreamReader and OutputStreamWriter 就干这事。转包byte stream--> character stream  。sockets 那个url里有演示

<http://java.sun.com/docs/books/tutorial/networking/index.html>

**Line-Oriented I/O**

面向行的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种结束符号 。支持多种结束符。有利于多个平台使用

**BufferedReader and PrintWriter. ** 这2个类有面向行的接口。。


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

```


 **outputStream.println(l);**

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. **

这段有点意思：注意调用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.