我正在尝试将文件从一个目录复制到另一个目录,一切正常,除非我尝试运行 Copy.exe 时出现异常。代码有什么问题?为什么条件为假时执行 if 语句的代码?
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Ah {
public static void main(String[] args){
File from = new File("."+"/original.exe");
File to = new File("/home/denis/Run/Copy.exe");
if(!to.exists()) {
to.getParentFile().mkdirs();
}
if(from.exists()){
FileChannel is = null;
try {
is = new FileInputStream(from).getChannel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileChannel os = null;
try {
os = new FileOutputStream(to).getChannel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.transferFrom(is, 0, is.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
for(File ff: new File(".").listFiles()){
//Why this code is not executed???
//why the condition is false but if clouse get executed??
//is the problem caused by something else???
System.out.print("There is no such file in a current working directory, except for "+ff+"\n");
}
}
}
}
这是我从终端运行“/home/denis/Run/Copy.exe”时遇到的异常
java.io.FileNotFoundException: /home/denis/Run/Copy.exe (Text file busy)
at gnu.java.nio.channels.FileChannelImpl.open(libgcj.so.10)
at gnu.java.nio.channels.FileChannelImpl.<init>(libgcj.so.10)
at gnu.java.nio.channels.FileChannelImpl.create(libgcj.so.10)
at java.io.FileOutputStream.<init>(libgcj.so.10)
at java.io.FileOutputStream.<init>(libgcj.so.10)
at Ah.main(Copy.exe)
Exception in thread "main" java.lang.NullPointerException
at Ah.main(Copy.exe)
我使用 GCJ 创建可执行文件,我在 ubuntu v-12 上运行 java 6