IO流

IO流

1.      输入/输出(针对内存)操作以“流”(stream)方式进行

2.      输入/输出功能

按数据流的方向不同可以分为输入流和输出流

按照处理数据单位不同可以分为字节流和字符流

按照功能不同可以分为节点流和处理流

3.      所有类型位于java.io内都分别继承以下4中抽象流类型

              字节流             字符流

输入流      InputStream         Reader

输出流      OutputStream        Writer

InputStream表示字节输入流的所有超类

OutputStream 表示字节输出流的所有超类

4.      节点流

可以从一个特定的数据(节点)读写数据(如:文件,内存)

5.      处理流

“连接”在已知存在的流(节点流或处理流)之上,通过数据的处理为程序提供更为强大的读写功能

6.      分隔符

Separator: 在不同系统上将斜杠转换成系统所需要的斜杠

pathSeparator:

7.      inputStream的基本用法

int read( ) throwsIOException

读一个字节并以整数形式返回(0--255)之间,如果返回-1已到输入流末尾

int  read(byte [ ] buffer) throws  IOException

读取length字节,并存储到一个字节数组buffer,返回实际读取的字节数,如果读取前已到输入流末尾返回-1

Int  read(byte [ ] buffer, int off , int len )throws IOException

读取length字节,并存储到一个字节数组buffer,从off位置开始存,最多len,返回实际读取的字节数,如果读取前已到输入流末尾返回-1

Void colose() throwsIOException

关闭流释放内存资源

8.      OutputStream基本用法

向输出流中写入一个字节数据,该字节数据为参数b的低8位

void writer( int b ) throwsIOException

 

将一字节类型的数组中的数据写入输出流

void  writer (byte [ ]  b) throws IOException

 

将一字节类型的数组中的数据从指定位置(off)开始的len个字节写入输出

Void writer(byte [ ] b, intoff ,  int len)  throws IOException

 

将输出流中缓冲的数据全部写出到目的地

Void flush()

 

关闭流释放资源

Void close() throwsIOException

9.      Reader的基本方法

//读取一个字符并以整数的形式返回(0 到 65535),

//如果返回-1已到输入流的末尾。

int read() throws IOException

 

//读取一系列字符并存储到一个数组buffer,

//返回实际读取的字符数,如果读取前已到输入流的末尾返回-1

int read(char[] cbuf) throws IOException

 

//读取length个字符

//并存储到一个数组buffer,从off位置开始存,最多读取len

//返回实际读取的字符数,如果读取前以到输入流的末尾返回-1

int read(char[] cbuf, int off, int len)

                     throws IOException

//关闭流释放内存资源

void close() throws IOException

10.Writer

//向输出流中写入一个字符数据,该字节数据为参数b的低16位

void write(int c) throws IOException

//将一个字符类型的数组中的数据写入输出流,

void write(char[] cbuf) throws IOException

//将一个字符类型的数组中的从指定位置(offset)开始的

//length个字符写入到输出流

void write(char[] cbuf, int offset, intlength)

                      throws IOException

//将一个字符串中的字符写入到输出流

void write(String string) throwsIOException

//将一个字符串从offset开始的length个字符写入到输出流

void write(String string, int offset, intlength)

                      throws IOException

//关闭流释放内存资源

void close() throws IOException

//将输出流中缓冲的数据全部写出到目的地

void flush() throws IOException

 

 

11.节点流类型

类型

字 符 流

字 节 流

File(文件)

FileReadr

FileWriter

FileInputStream

FileOutputStream

Memory Array

CharArrayReader

CharArrayWriter

ByteArrayInputStream

ByteArrayOutputStream

Memory String

StringReader

StringWriter

Pipe(管道)

PipedReader

PipedWriter

PipedInputStream

PipedOutputStream

         12. FileInputStreamFileOutputStream分别继承自InputStreamOutputStream用于向文件中输入和输出字节

Ø FileInputStream和FileOutputStream的常用构造方法:

FileInputStream(String name) throwsFileNotFoundException

FileInputStream(File file)   throws FileNotFoundException

FileOutputStream(String name)throwsFileNotFoundException

FileOutputStream(File file)  throws FileNotFoundException

FileOutputStream(Filefile,booleanappend)throwsFileNotFoundException

Ø       FileInputStreamFileOutputStream类支持其父类InputStream OutputStream 所提供的数据读写方法。

在读写时要注意:

在实例化FileInputStream和FileOutputSteam流时要用try-catch语句以处理其可能抛出的FileNotFoundException。

Ø 在读写数据时也要用try-catch语句以处理可能抛出的 IOException。

Ø FileNotFoundException是IOException的子类

13.  FileReader 和 FileWriter 分别继承自Reader和Writer,FileInputSteam与FileOutputStream类似,所不同的时FileReader和FileWriter向文件输入和输出的数据单位为字符

FileReaderFileWriter的常用构造方法

public FileWriter(File file) throwsIOException

public FileWriter(File file, booleanappend)

                        throws IOException

public FileWriter(String fileName)throwsIOException

public FileWriter(String fileName,booleanappend)

                        throws IOException

public FileReader(String fileName)

                        throwsFileNotFoundException

public FileReader(File file)

                        throwsFileNotFoundException

 

 

 

 

14. 流处理类型

处理类型

字 符 流

字 节 流

Buffering

BufferedReader

BufferedWriter

BufferedInputStream

BufferedOutputStream

Filtering

FilterReader

FilterWriter

FilterInputStream

FilterOutputStream

Converting between bytes and character

InputStreamReader

OutputStreamWriter

 

Object Serialization

ObjectInputStream

ObjectOutputStream

Data conversion

DataInputStream

DataOutputStream

Counting

LineNumberReader

LineNumberInputStream

Peeking ahead

PusbackReader

PushbackInputStream

Printing

PrintWriter

PrintStream

15.缓冲流

缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率

       四种缓冲流的构造方法如下:

BufferedReader(Reader in)    

BufferedReader(Reader in,int sz) //sz 为自定义缓存区的大小

BufferedWriter(Writer out)

BufferedWriter(Writer out,int sz)

BufferedInputStream(InputStream in)

BufferedInputStream(InputStream in,intsize)

BufferedOutputStream(OutputStream out)

BufferedOutputStream(OutputStream out,intsize)

l BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)。

l BufferedWriter提供了newLine用于写入一个行分隔符。

l 对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush方法将会使内存中的数据立刻写出。

16. 数据流ByteArrayInputStream&ByteArrayOutputStream

ByteArrayInputStream主要方法

ByteArrayInputStream(byte[] buf) ; 创建一个ByteArrayInputStream,使用 buf 作为其缓冲区数组。read(byte[] b,int off, int len) 将最多 len个数据字节从此输入流读入 byte 数组close()

         ByteArrayOutputStream 主要方法:

                     ByteArrayOutputStream(intsize) ;创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量(以字节为单位)。toByteArray()  创建一个新分配的 byte 数组。(新增方法,不要使用多态)

17. 字节转化流InputStreamRead和OutputStreamWriter

InputStreamReader(InputStream in, CharsetDecoder dec)

   创建使用给定字符集解码器的InputStreamReader。

OutputStreamWriter(OutputStreamout, CharsetEncoder enc)

   创建使用给定字符集编码器的OutputStreamWriter

18. print 流(输出类型的流,不抛出异常,自动flush

              PrintWriter和PrintStream 都属于输出流,分别针对与字符和字节

PrintWriter和PrintStream提供了重载的print

              Println方法用于多种数据类型的输出

              PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息。

              PrintWriter和PrintStream有自动flush功能

19. printWriter和printStream常用方法

       PrintWriter(Writer out)

       PrintWriter(Writer out,boolean autoFlush)

PrintWriter(OutputStreamout)

PrintWriter(OutputStreamout,boolean autoFlush)

PrintStream(OutputStreamout)

PrintStream(OutputStreamout,booleanautoFlush)

20.Object流

       直接将Object写入(ObjectInputStream)或读出(ObjectOutputStream)

       transient关键字

       serializable接口--à标记性接口

       Externalizable 接口--à自己控制序列化

       void writeExternal(ObjectOutput out) throwsIOException

       void readExternal(ObjectInput in) throwsIOException, ClassNotFoundException

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值