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 CopyCharactersuse 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

  1. FileInputStream fis;  
  2.         try {  
  3.             fis = new FileInputStream("E:/f.txt");  
  4.             InputStreamReader isr=new InputStreamReader(fis);  
  5.             BufferedReader br=new BufferedReader(isr);  
  6.             String line=null;  
  7.             while((line=br.readLine())!=null){  
  8.                 System.out.println(line);  
  9.             }  

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 (" "), a single carriage-return (" "), or a single line-feed (" "). Supporting all possible line terminators allows programs to read text files created on any of the widely used operating systems.

---遇到“ ”单个换行符 和 " "单个回车键  代表一行的结束

[java] view plaincopy

  1. public class CopyLines {  
  2.     public static void main(String[] args) throws IOException {  
  3.   
  4.   
  5.         BufferedReader inputStream = null;  
  6.         BufferedWriter outputStream = null;  
  7.   
  8.   
  9.         try {  
  10.             inputStream = new BufferedReader(new FileReader("e:/f.txt"));  
  11.             outputStream = new BufferedWriter(new FileWriter("e:/b.txt"));  
  12.   
  13.   
  14.             String l;  
  15.             while ((l = inputStream.readLine()) != null) {  
  16.                outputStream.write(l);  
  17.             }  
  18.         } finally {  
  19.             if (inputStream != null) {  
  20.                 inputStream.close();  
  21.             }  
  22.             if (outputStream != null) {  
  23.                 outputStream.close();  
  24.             }  
  25.         }  
  26.     }  
  27. }  

Invoking readLine returns a line of text with the line.--调用返回文本的每一行

重生之大文豪 www.dwhao.com
原文地址:https://www.cnblogs.com/jiangye/p/3306087.html