java中提供了处理以16位的Unicode码表示的字符流的类,即以Reader和Writer 为基类派生出的一系列类。 

1.Reader和Writer   

  这两个类是抽象类,只是提供了一系列用于字符流处理的接口,不能生成这两个类的实例,只能通过使用由它们派生出来的子类对象来处理字符流。 

public int read() throws IOException 读取一个字符,返回值为读取的字符
public int read(char cbuf[]) throws IOException 读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量
public abstract int read(char cbuf[],int off,int len) throws IOException 读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现
public boolean markSupported() 判断当前流是否支持做标记
public void mark(int readAheadLimit) throws IOException 给当前流作标记,最多支持readAheadLimit个字符的回溯
public void reset() throws IOException 将当前流重置到做标记处 
public void write(int c) throws IOException 将整型值c的低16位写入输出流
public void write(char cbuf[]) throws IOException 将字符数组cbuf[]写入输出流 
public abstract void write(char cbuf[],int off,int len) throws IOException 将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流
public void write(String str) throws IOException 将字符串str中的字符写入输出流
public void write(String str,int off,int len) throws IOException 将字符串str 中从索引off开始处的len个字符写入输出流
flush( )  刷空输出流,并输出所有被缓存的字节
public abstract void close() throws IOException;  关闭流 

Java中的流(3)字符流-Reader和Writer

Java中的流(3)字符流-Reader和Writer

2. InputStreamReader和OutputStreamWriter 

public InputStreamReader(InputStream in) in是字节流,而InputStreamReader是字符流,但是其来源是字节流in, 
因此InputStreamReader就可以把字节流in转换成字符流处理。
public InputStreamReader(InputStream in,String enc) throws UnsupportedEncodingException enc是编码方式,就是从字节流到字符流进行转换时所采用的编码方式, 
例如 ISO8859-1,UTF-8,UTF-16等等
public OutputStreamWriter(OutputStream out) out是字节流,而OutputStreamReader是字符流
public OutputStreamWriter(OutputStream out,String enc) throws UnsupportedEncodingException enc是编码方式 
public String getEncoding() 获取当前编码方式 
读入和写出字符 基本同Reader和Writer

3. BufferedReader和BufferedWriter 

public BufferedReader(Reader in) 使用缺省的缓冲区大小
public BufferedReader(Reader in, int sz) sz为缓冲区的大小
public BufferedWriter(Writer out) public BufferedWriter(Writer out, int sz)
public String readLine() throws IOException 读一行字符 
public void newLine() throws IOException 除了Reader和Writer中提供的基本的读写方法外,增加对整行字符的处理
读入/写出字符  基本同Reader和Writer

4.示例

 1 import java.io.*;
 2 
 3 public class NumberInput {
 4     public static void main(String args[]) {
 5         try {
 6             InputStreamReader ir;
 7             BufferedReader in;
 8             ir = new InputStreamReader(System.in);
 9             // 从键盘接收了一个字符串的输入,并创建了一个字符输入流的对象
10             in = new BufferedReader(ir);
11             String s = in.readLine();
12             // 从输入流in中读入一行,并将读取的值赋值给字符串变量s
13             System.out.println("Input value is: " + s);
14             int i = Integer.parseInt(s);// 转换成int型
15             i *= 2;
16             System.out.println("Input value changed after doubled: " + i);
17         } catch (IOException e) {
18             System.out.println(e);
19         }
20     }
21 }

运行结果 

D:/>java NumberInput 
123 
Input value is 123 
Input value changed after doubled: 246 

注意:在读取字符流时,如果不是来自于本地的,比如说来自于网络上某处的与本地编码方式不同的机器,那么我们在构造输入流时就不能简单地使用本地缺省的编码方式,否则读出的字符就不正确;为了正确地读出异种机上的字符,我们应该使用下述方式构造输入流对象: 

ir = new InputStreamReader(is, "8859_1"); 

采用ISO 8859_1编码方式,这是一种映射到ASCII码的编码方式,可以在不同平台之间正确转换字符。 

 

相关文章:

  • 2021-06-19
  • 2021-10-21
  • 2022-12-23
  • 2021-05-23
  • 2021-07-13
  • 2022-12-23
  • 2021-08-27
  • 2022-01-19
猜你喜欢
  • 2021-07-30
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-01
相关资源
相似解决方案