I/O 文件间双缓存复制
大神帮看看,新手求教!!!
下边代码写出来,执行之后,输出文件内容没了,输入的文件里边也没有内容...
帮忙看看是哪里错了!
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);
}
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(true){
i = bis.read();
if (i > 0) break;
bos.write(i);
}
break;
}else{
bis.read(dataBytes);
bos.write(dataBytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
bos.flush(); //强制清除缓存内容
bis.close();
bos.close();
}
[其他解释]
完全不明白为什么要这么麻烦
[其他解释]
#2
我是不会啊,我也不知道啊,我感觉这样能行,但是不行
[其他解释]
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((i = bis.read(dataBytes)) != -1){
bos.write(dataBytes, 0, i);
}
bos.flush(); // 强制清除缓存内容
bis.close();
bos.close();
}