黑马程序员_IO

 ------- android培训java培训、期待与您交流! ----------


IO流

(1)分类

字节流:

输入流:

InputStream

int read()

int read(byte[] bys)

FileInputStream

BufferedInputStream

输出流:

OutputStream

write(int by)

write(byte[] bys,int index,int len)

FileOutputStream

BufferedOutputStream

 

字符流:

输入流:

Reader

int read()

int read(char[] chs)

FileReader

BufferedReader

String readLine()

输出流:

Writer

write(int ch)

write(char[] chs,int index,int len)

FileWriter

BufferedWriter

write(String line)

void newLine()

(2)到底使用谁?

用记事本打开能读懂的,就用字符流。

否则,用字节流。

(3)复制文本文件:

9种方式:

字节流:

4种

FileInputStream fis = new FileInputStream("c:\\a.txt");

FileOutputStream fos = new FileOutputStream("c:\\a.txt");

基本流一次读写一个字节

 int by = 0;

 while((by=fis.read())!=-1)

 {

 fos.write(by);

 }

基本流一次读写一个字节数组

 

 int by = 0;

 while((by=fis.read())!=-1)

 {

 fos.write(by);

 }

 

 

 

高效流一次读写一个字节

高效流一次读写一个字节数组

和基本流只是创建对象不同

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\a.txt"));

BufferedOutputStream bos = new BufferedOutputStream(

new FileOutputStream("c:\\a.txt"));

 

字符流:

5种

基本流一次读写一个字符

基本流一次读写一个字符数组

高效流一次读写一个字符

高效流一次读写一个字符数组

高效流一次读写一个字符串

 

一般来说,明明知道是文本文件,那么,肯定不用字节流。

那么,如果是使用字符流,有5种方式,选择哪一种呢?

一般都选择高效流读写一个字符串的方式。

 

 

代码体现:

数据源:c:\\a.txt

目的地:d:\\b.txt

 

BufferedReader br = new BufferedReader(new FileReader("c:\\a.txt"));

BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\b.txt"));

 

String line = null;

while((line=br.readLine())!=null)

{

bw.write(line);

bw.newLine();

bw.flush();

}

 

bw.close();

br.close();

 

(4)复制二进制流数据:(图片,视频,音频等)

字节流:

4种

基本流一次读写一个字节

基本流一次读写一个字节数组

高效流一次读写一个字节

高效流一次读写一个字节数组

 

一般来说,我们选择使用高效流一次读写一个字节数组

 

代码体现:

数据源:c:\\a.jpg

目的地:d:\\b.jpg

 

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\a.jpg"));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\b.jpg"));

 

byte[] bys = new byte[1025];

int len = 0;

while((len=bis.read(bys))!=-1)

{

bos.write(bys,0,len);

}

 

bos.close();

bis.close();

 ------- android培训java培训、期待与您交流! ----------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值