Copy the path directly, and then run, an error will occur.
The error is as follows, I don't know why.
After that, re-enter the same path and the error disappeared.
Wrong code
package try_a_package;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
class copyFirst {
public static void main(String args[])
{
// 得到当前工作目录
String curPath=System.getProperty("user.dir");
System.out.println(curPath);
// 文件输入输出
try {
File f1=new File("D:\\file1.txt");
File f2=new File("D:\\file2.txt");
FileInputStream in = new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
System.out.println(in.available());
while(in.available()>0)
{
int ch=in.read();
out.write(ch);
}
in.close();
out.close();
System.out.println("Success");
}
catch(FileNotFoundException e) {
System.out.println("错误,该文件打不开!");
}
catch(IOException e) {
System.out.println("错误,文件不能读写!");
}
}
}
Correct code
package try_a_package;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
class copyFirst {
public static void main(String args[])
{
// 得到当前工作目录
String curPath=System.getProperty("user.dir");
System.out.println(curPath);
// 文件输入输出
try {
File f1=new File("D:\\file1.txt");
File f2=new File("D:\\file2.txt");
FileInputStream in = new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
System.out.println(in.available());
while(in.available()>0)
{
int ch=in.read();
out.write(ch);
}
in.close();
out.close();
System.out.println("Success");
}
catch(FileNotFoundException e) {
System.out.println("错误,该文件打不开!");
}
catch(IOException e) {
System.out.println("错误,文件不能读写!");
}
}
}
These two codes are exactly the same in CSDN, but after I copied and ran, an error did occur. I don't know the principle.
Blind guessing: Is there a coding problem?
(Big guy teaches me!)