java 语言导学_Java语言导学笔记 Chapter 9 IO

java.io

9.1.1 字符流

Reader为读取器(reader)提供API和部分实现,读取器是读取16位字符的流;

Writer为写出器(writer)提供API和部分实现,写出器是写16位字符的流。

一般用来读写文本信息,因为可以处理Unicode字符集中的任何字符,而字节流只能处理ISO-Latin-18位字节??

9.1.2 字节流

读写8位字节,用InputStream和OutputStream的后代。

InputStream和OutputStream为输入流(input stream,读取8位字节的流)和输出流(output stream,写8位字节流)提供部分API和部分实现。通常用于读写二进制数据,比如图象和声音。

9.1.3 理解I/O超类

所有流都在创建时自动打开,应该通过调用流的close方法显示地关闭流,否则垃圾收集器会自动地关闭它。

9.2 使用流

I/O类型:内存,管道,文件,联结,对象串行化,数据转换,计数,预览,打印,缓冲,过滤,在字节和字符之间进行转换。

9.2.1 如何使用文件流

文件流FileReader, FileWriter, FileInputStream和FileOutputStream都对本机文件系统上的一个文件进行读写。可以从一个文件名创建文件流,文件名的形式可以是字符串、File对象或FileDescriptor对象。

import java.io.*;

public class Copy{

public static void main(String[] args)throws IOException{

File inputFile = new File("farrago.txt");

File outputFile = new File("outagain.txt");

FileReader in = new FileReader(inputFile);

FileWriter out = new FileWriter(outputFile);

int c;

while((c = in.read()) != -1){

out.write(c);

}

in.close();

out.close();

}

}

FileReader和FileWriter读写16位的字符。在进行读写时,这些流根据默认的字符编码对字符进行编码。通过调用System.getProperty(“file.encoding”)可以得到默认字符编码的名称。要想指定默认字符编码之外的另一种字符编码,应该在一个FileOutputStream上构造一个OutputStreamWriter并指定编码。

9.2.2 如何使用管道流

管道用于将一个线程的输出链接到另一个线程的输入。

程序RhymingWords使用PipedReader和PipedWriter将它的reverse和sort方法的输入输出连接起来一遍创建一个”押韵”的单词列表。

程序见RhymingWords.java。

FileReader words = new FileReader("words.txt");

Reader rhymedWords = reverse(sort(reverse(words)));

其中reverse方法:

public static Reader reverse(Reader source) throws IOException {

BufferedReader in = new BufferedReader(source);

PipedWriter pipeOut = new PipedWriter();

PipedReader pipeIn = new PipedReader(pipeOut);

PrintWriter out = new PrintWriter(pipeOut);

new ReverseThread(out, in).start();

return pipeIn;

}

创建管道的两端(PipedWriter和PipedReader),并且通过在PipedWriter上构造PipedReader将它们连接起来。写到PipedWriter上的内容都从PipedReader读取。这个连接就组成了管道。

reverse方法启动一个ReserveThread线程,它将它的输出写到PipedWriter并向调用者返回PipedReader。然后调用者安排一个线程从PipedReader读取数据。

9.2.3 如何封装流

BufferedReader in = new BufferedReader(source);

……

PrintWriter out = new PrintWriter(pipeOut);

此代码在source上打开一个BufferedReader,source是另一个不同类型的读取器。这本质上就是将source封装到BufferedReader中。程序从BufferedReader读取,BufferedReader进而从source读取。这么做就可以使用BufferedReader的方便的readLine方法。

9.2.4 如何联结文件

SequenceInputStream从多个输入源创建单个输入流?

import java.io.*;

public class Concatenate{

public static void main(String[] args) throws IOException{

ListOfFiles mylist = new ListOfFiles(args);

SequenceInputStream s = new SequenceInputStream(mylist);

int c;

while((c = s.read()) != -1){

System.out.write(c);

}

s.close();

}

}

Concatenate首先创建一个名为mylist的ListOfFiles对象,这个对象由用户输入的命令行参数初始化。mylist对象是一个枚举,SequenceInputStream在需要时使用这个mylist对象得到一个新的InputSream。

9.2.5 操作过滤流

过滤器流在从流中读写数据时过滤数据。过滤器流包括FileReader, FileWrriter, FileInputStream和FileOutputStream。

1.  使用过滤器流

在创建过滤器流时将它附加到另一个输入或输出流。例如:将DataInputStream附加到标准输入流:

DataInputStream in = new DataInputStream(System.in);

String input;

while ((input = in.readLine()) != null){

}

这么做可以使用更多由DataInputStream实现的方便的读取方法,比如readLine。

2.  如何使用DataInputStream和DataOutputStream

3.  如何编写自己的过滤器流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值