mercredi 26 août 2015

If block get executed while condition is false

I am trying to copy a file from one directory to another, everything works perfect except when i try to run Copy.exe am getting an exception. What wrong with the code? why the code withing if statement is executed while the condition is false??

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");
        }
      }
    }
}

This is the exception am getting when i run "/home/denis/Run/Copy.exe" from terminal

 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)

I use GCJ to create executable and i am running java 6 on ubuntu v-12

Aucun commentaire:

Enregistrer un commentaire