日期:2014-05-20 浏览次数:20760 次
package Java12_10_18;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferStream1 {
/*
* 双缓冲复制BufferInputStream&BufferOutputStream
*/
public static void main(String[] args) throws IOException{
File inFile = new File("F:\\201021510513\\java\\java3.txt");
File outFile = new File("F:\\201021510513\\java\\java2.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int i = 0;
byte[] dataBytes = new byte[1024];
while(true){
try {
if(bis.available()<1024){
while(i!=-1){
i = bis.read();
bos.write(i);
}
break;
}else{
bis.read(dataBytes);
bos.write(dataBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
bos.flush(); //强制清除缓存内容
bis.close();
bos.close();
}
}
}
if(bis.available()<1024){
while(i!=-1){
i = bis.read();
bos.write(i);
}
break;
}else{
bis.read(dataBytes);
bos.write(dataBytes);
}