byte[] strByte = new byte[(int)f1.length()]; 这一行修改为 byte[] strByte = new byte[(int)f2.length()]; 妹的,看你这代码f1,f2的,自己都搞乱了不是??
变量命名很重要的,为什么不用src,des来区分源文件和目标文件?用什么f1,f2,乱不乱
变量命名很重要的,为什么不用src,des来区分源文件和目标文件?用什么f1,f2,乱不乱
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class FileStream {
public static void main(String[] args) {
File f1 = new File("F:\\java.txt");
String str = "F:\\java1.txt";
File f2 = new File(str);
if(f2.isFile()){
System.out.println("文件java1.txt存在,下面进行文件输入!");
}else{
try {
f2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(f1);//这两个地方变量写错了
fo = new FileOutputStream(f2);
byte[] strByte = new byte[(int)f1.length()];
while(fi.read(strByte)>-1){ //此处不是!=-1,是>-1
String str1=new String(strByte);
System.out.println(str1);
fo.write(strByte);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fi.close();
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这句回答错了,是fi.read(strByte)!=-1