JavaIO流-缓冲流

import org.junit.Test;

import java.io.*;

/**
*
* 处理流之一缓冲流
* 1.BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
* 2.作用:提高流的读写速度
* 原因:内部提供一个缓冲区
*
*3.处理流:就是“套接”在已有流的基础上。
* @author orz
*/


public class BufferedTest {
/**
* BufferedInputStream、BufferedOutputStream综合使用
*
*/
@Test
public void test1()
{

FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
File file1=new File("picture1.png");
File file2=new File("picture2.png");
fis = new FileInputStream(file1);
fos=new FileOutputStream(file2);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);

byte [] buffer=new byte[1024];
int len;
while ((len=bis.read(buffer))!=-1)
{
bos.write(buffer,0,len);
}

} catch (IOException e) {
e.printStackTrace();
}
finally {
//资源关闭
//要求:先关外层,再关里层
//关闭外层流的同时内层流也会自动的进行关闭,关于内层流的关闭,我们可以不管
try {
if(bos!=null)
{
bos.close();
}

} catch (IOException e) {
e.printStackTrace();
}
try {
if(bis!=null)
{
bis.close();
}

} catch (IOException e) {
e.printStackTrace();
}
}

}


/**
*
* 实现文件复制
*/
public void copyFileWithBuffered(String srcPath,String destPath)
{
FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
File file1=new File(srcPath);
File file2=new File(destPath);
fis = new FileInputStream(file1);
fos=new FileOutputStream(file2);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);

byte [] buffer=new byte[1024];
int len;
while ((len=bis.read(buffer))!=-1)
{
bos.write(buffer,0,len);
}

} catch (IOException e) {
e.printStackTrace();
}
finally {
//资源关闭
//要求:先关外层,再关里层
//关闭外层流的同时内层流也会自动的进行关闭,关于内层流的关闭,我们可以不管
try {
if(bos!=null)
{
bos.close();
}

} catch (IOException e) {
e.printStackTrace();
}
try {
if(bis!=null)
{
bis.close();
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

@Test
public void copyTest()
{
String srcPath="E:\\1.mp4";
String destPath="E:\\2.mp4";
long start=System.currentTimeMillis();
copyFileWithBuffered(srcPath,destPath);
long end=System.currentTimeMillis();
System.out.println("复制花费操作时间为"+(end-start));//546
}

/**
* BufferedReader、BufferedWriter综合使用
*
*/
@Test
public void test2()
{
//1.造文件
//2.造流
//3.读写数据
//4.资源关闭

//2.造流
FileReader fr=null;
FileWriter fw=null;
BufferedReader br=null;
BufferedWriter bw=null;

try {
//1.造文件
File file1=new File("hello.txt");
File file2=new File("hi.txt");

fr=new FileReader(file1);
fw=new FileWriter(file2);

br=new BufferedReader(fr);
bw=new BufferedWriter(fw);

//3.读写数据
/*
//方式一
char [] cbuf=new char[1024];
int len;
while ((len=br.read(cbuf))!=-1)
{
// String str=new String(cbuf,0,len);
// System.out.print(str);
bw.write(cbuf,0,len);
}*/
//方式二
String str;
while ((str=br.readLine())!=null)
{
//不自动换行
// bw.write(str+"\n");
bw.write(str);
bw.newLine();
}

}catch (IOException e)
{
e.printStackTrace();
}
finally {
//4.资源关闭
try {
if(br!=null)
{
br.close();
}

} catch (IOException e) {
e.printStackTrace();
}
try {
if(bw!=null)
{
bw.close();
}

} catch (IOException e) {
e.printStackTrace();
}
}


}
}
posted @ 2020-07-29 17:31  orz江小鱼  阅读(99)  评论(0编辑  收藏  举报