InputStream与OutputStream的使用例子

public class Test_two {

public static void main(String[] args) throws Exception{
/**
 * InputStream与OutputStream的使用例子
 * 
 * (缓冲文件输入流)BufferedInputStream → (文件输入流)FileInputStream → (输入流)java.io.InputStream
 * 
 * (缓冲文件输出流)BufferedOuputStream → (文件输出流)FileOuputStream → (输出流)java.io.OutputStream
 */
/**
 * 1.通过流复制一个图片的例子
 */
/* File file = new File("c:/images/1.png");
File outfile = new File("C:/temp.png");
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(outfile);
int i = 0;
while(i != -1) {
i = inputStream.read();
outputStream.write(i);
}
//注意流的关闭(★必须的)
inputStream.close();
outputStream.close();
*/
/**
 * 2.如果我们想提高要提高复制的速度,可以采用缓冲文件输入\输出流,如下:
 */
/* File file = new File("C:/images/1.png");
File outfile = new File("C:/temp1.jpg");
//文件输入流
FileInputStream inputStream = new FileInputStream(file);
//文件输出流
FileOutputStream outputStream = new FileOutputStream(outfile);
//缓冲文件输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//缓冲文件输出流
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
int i = 0;
while(i != -1) {
i = bufferedInputStream.read();
bufferedOutputStream.write(i);
}
//流的关闭
bufferedOutputStream.flush();//强制清除缓冲区的内容
bufferedInputStream.close();
bufferedOutputStream.close();
*/
/**
 * 3.当文件很大,我们要做一个缓冲处理来提高速度。如下:当文件的大小大于512个字节时,每次读入512个字节后再做处理
 * 
 */
/* File file = new File("C:/images/1.png");
File outfile = new File("C:/temp2.png");
//文件输入流
FileInputStream inputStream = new FileInputStream(file);
//文件输出流
FileOutputStream outputStream = new FileOutputStream(outfile);
int i = 0;
//缓冲大小为512字节
byte[] buffer = new byte[512];
while(true) {
if(inputStream.available() < 512) {
while(i != -1) {
i = inputStream.read();
outputStream.write(i);
}
break;//注意此处不能忘记哦
} else {
//当文件的大小大于512字节时
inputStream.read(buffer);
outputStream.write(buffer);
}
}
//流的关闭
//注意流的关闭(★必须的)
inputStream.close();
outputStream.close();
*/
/**
 * 4.根据上面的例子,我们可以知道:我们可以做一个双缓冲的文件复制
 */
File file = new File("C:/images/1.png");
File outfile = new File("C:/temp3.png");
//文件输入流
FileInputStream inputStream = new FileInputStream(file);
//文件输出流
FileOutputStream outputStream = new FileOutputStream(outfile);
//缓冲文件输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//缓冲文件输出流
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
int i = 0;
//缓冲区的大小
byte[] buffer = new byte[512];
while(true) {
if(bufferedInputStream.available() < 512) {
while(i != -1) {
i = bufferedInputStream.read();
bufferedOutputStream.write(i);
}
break;
} else {
//当文件的大小还大于512字节时
bufferedInputStream.read(buffer);
bufferedOutputStream.write(buffer);
}
}
//强制清空缓冲区的内容
bufferedOutputStream.flush();
//流的关闭
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值