1. 程式人生 > 實用技巧 >JavaIO流-緩衝流

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();
}
}


}
}