Java 输入输出流

  

IO流的三种分类方式

1.按流的方向分为:输入流和输出流

 

2.按流的数据单位不同分为:字节流和字符流

 

3.按流的功能不同分为:节点流和处理流

 

(节点流表示的是直接操作节点(例如文件,键盘)的流,例如FileInputStream.
处理流(过滤流)表示的是对节点流进行了操作(加工)的类,例如InputStreamReader)

IO流的四大抽象类

字符流:Reader(读)Writer(写)

 

字节流:InputStream(读数据) OutputStream(写数据)

 

Java <wbr>输入输出流 <wbr>汇总

                               IO流对象继承关系

Java <wbr>输入输出流 <wbr>汇总

                      其它常用与流有关的对象

Java <wbr>输入输出流 <wbr>汇总

                           节点流类型

 

IO体系,所具备的基本功能就有两个:读和写

1,字节流

InputStream(读),OutputStream(写)

2,字符流

Reader(读),Writer(写)

 

结论:只要是处理纯文本数据,就要优先考虑使用字符流,除此之外都用字节流。

 

InputStream的基本方法

int read()throws IOException 读取一个字节以整数形式返回,如果返回-1已到输入流的末尾

voidclose() throws IOException 关闭流释放内存资源

longskip(long n) throws IOException 跳过n个字节不读

 

OutputStream的基本方法

voidwrite(int b) throws IOException 向输出流写入一个字节数据

voidflush() throws IOException 将输出流中缓冲的数据全部写出到目的地

 

Reader的基本方法

int read()throws IOException 读取一个字符以整数形式返回,如果返回-1已到输入流的末尾

 

Writer的基本方法

voidwrite(int c) throws IOException 向输出流写入一个字符数据

voidwrite(String str) throws IOException将一个字符串中的字符写入到输出流

voidwrite(String str,int offset,int length)

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

voidflush() throws IOException

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

缓冲流:缓冲流要套接在相应的节点流之上,提高了读写的效率。

此处理流的构造方法都得传相对应的基类类型

BufferedReader:提供了readLine方法用于高校读取一行字符串

BufferedWriter:提供了newLine用于写入一个行分隔符也就是换行

BufferedInputStream 没多大用处

BufferedOutputStream 没多大用处

转换流:主要作用将字节流转换成字符流。用处较大!

转换流在构造时可以指定其编码集合

InputStreamReader需要和InputStream套接

OutputStreamWriter需要和OutputStream套接

例:OutputStreamWriter osw= new OutputStreamWriter (newFileOutputStream(文件路径);

方法例:osw.getEncoding();获得流的编码方式

 

数据流与字节数组流

数据流主要为实现可以存取Java原始数据类型如long,boolean

数据流是字节流

DataInputStream需要和InputStream套接

DataOutputStream需要和OutputStream套接

DataInputStream方法:readBoolean()readInt()…

readUTF():网络传输常用方法读一个Unicode字符串

DataOutputStream方法与DataInputStream基本对应为写的方法

//此构造函数等于已可以往一个字节数组里输入内容

ByteArrayOutputStream baos = newByteArrayOutputStream ();

 

//此方法为获取一个字节数组方法返回字节数组

baos.toByteArray();

 

//此方法获取字节数组占了多少字节

newByteArrayInputStream(一个字节数组)。available()

ByteArrayOutputStream baos = newByteArrayOutputStream();

DataOutputStream dos = newDataOutputStream(baos);

try

{

      dos.writeDouble(Math.random());

      dos.writeBoolean(true);

      ByteArrayInputStream bais = newByteArrayInputStream(baos.toByteArray());

     System.out.println(bais.available());

     DataInputStream dis = new DataInputStream(bais);

     System.out.println(dis.readDouble());

     System.out.println(dis.readBoolean());

     dos.close();

dis.close();

 }

 catch (IOExceptione)

 {

     e.printStackTrace();

 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值