核心提示:stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。
一. Input和Output
1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型:
1.1 以字节为导向的stream
以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面 几种类型:
1) input stream:
1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream:把一个String对象作为InputStream
3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream:把多个InputStream合并为一个InputStream
2) Out stream
1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream:把多个OutStream合并为一个OutStream
1.2 以Unicode字符为导向的stream
以Unicode字符为导向的stream,表示以Unicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种类型:
1) Input Stream
1) CharArrayReader:与ByteArrayInputStream对应
2) StringReader:与StringBufferInputStream对应
3) FileReader:与FileInputStream对应
4) PipedReader:与PipedInputStream对应
2) Out Stream
1) CharArrayWrite:与ByteArrayOutputStream对应
2) StringWrite:无与之对应的以字节为导向的stream
3) FileWrite:与FileOutputStream对应
4) PipedWrite:与PipedOutputStream对应
以字符为导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如CharArrayReader:和ByteArrayInputStream的作用都是把内存中的一个缓冲区作为InputStream使用,所不同的是前者每次从内存中读取一个字节的信息,而后者每次从内存中读取一个字符。
1.3 两种不现导向的stream之间的转换
InputStreamReader和OutputStreamReader:把一个以字节为导向的stream转换成一个以字符为导向的stream。
2. stream添加属性
2.1 “为stream添加属性”的作用
运用上面介绍的Java中操作IO的API,我们就可完成我们想完成的任何操作了。但通过FilterInputStream和FilterOutStream的子类,我们可以为stream添加属性。下面以一个例子来说明这种功能的作用。
如果我们要往一个文件中写入数据,我们可以这样操作:
FileOutStreamfs=newFileOutStream(“test.txt”); |
然后就可以通过产生的fs对象调用write()函数来往test.txt文件中写入数据了。但是,如果我们想实现“先把要写入文件的数据先缓存到内存中,再把缓存中的数据写入文件中”的功能时,上面的API就没有一个能满足我们的需求了。但是通过FilterInputStream和FilterOutStream的子类,为FileOutStream添加我们所需要的功能。
2.2FilterInputStream的各种类型
2.2.1用于封装以字节为导向的InputStream
1)DataInputStream:从stream中读取基本类型(int、char等)数据。
2)BufferedInputStream:使用缓冲区
3)LineNumberInputStream:会记录inputstream内的行数,然后可以调用getLineNumber()和setLineNumber(int)
4)PushbackInputStream:很少用到,一般用于编译器开发
2.2.2用于封装以字符为导向的InputStream
1)没有与DataInputStream对应的类。除非在要使用readLine()时改用BufferedReader,否则使用DataInputStream
2)BufferedReader:与BufferedInputStream对应
3)LineNumberReader:与LineNumberInputStream对应
4)PushBackReader:与PushbackInputStream对应
2.3FilterOutStream的各种类型
2.2.3用于封装以字节为导向的OutputStream
1)DataIOutStream:往stream中输出基本类型(int、char等)数据。
2)BufferedOutStream:使用缓冲区
3)PrintStream:产生格式化输出
2.2.4用于封装以字符为导向的OutputStream
1)BufferedWrite:与对应
2)PrintWrite:与对应
3.RandomAccessFile
1)可通过RandomAccessFile对象完成对文件的读写操作
2)在产生一个对象时,可指明要打开的文件的性质:r,只读;w,只写;rw可读写
3)可以直接跳到文件中指定的位置
4.I/O应用的一个例子
importjava.io.*;
publicclassTestIO{ publicstaticvoidmain(String[]args) throwsIOException{ //1.以行为单位从一个文件读取数据 BufferedReaderin= newBufferedReader( newFileReader("F:\nepalon\TestIO.java")); Strings,s2=newString(); while((s=in.readLine())!=null) s2+=s+" "; in.close();
//1b.接收键盘的输入 BufferedReaderstdin= newBufferedReader( newInputStreamReader(System.in)); System.out.println("Enteraline:"); System.out.println(stdin.readLine());
//2.从一个String对象中读取数据 StringReaderin2=newStringReader(s2); intc; while((c=in2.read())!=-1) System.out.println((char)c); in2.close();
//3.从内存取出格式化输入 try{ DataInputStreamin3= newDataInputStream( newByteArrayInputStream(s2.getBytes())); while(true) System.out.println((char)in3.readByte()); } catch(EOFExceptione){ System.out.println("Endofstream"); }
//4.输出到文件 try{ BufferedReaderin4= newBufferedReader( newStringReader(s2)); PrintWriterout1= newPrintWriter( newBufferedWriter( newFileWriter("F:\nepalon\TestIO.out"))); intlineCount=1; while((s=in4.readLine())!=null) out1.println(lineCount+++":"+s); out1.close(); in4.close(); }
|