如果在条件为假时执行块

时间:2015-08-27 03:17:36

标签: java file if-statement filechannel gcj

我正在尝试将文件从一个目录复制到另一个目录,一切都很完美,除非我尝试运行Copy.exe时遇到异常。代码有什么问题?为什么在条件为false的情况下执行带有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

0 个答案:

没有答案