0

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

deyur
  • 557
  • 2
  • 14
Denis
  • 390
  • 3
  • 14
  • 1
    The `FileNotFoundException` is thrown for your `to` file. And your checking `from.exists()`. So there's nothing wrong with if statement. The Copy.exe file is not editable (or not creatable) for some reason. – Codebender Aug 27 '15 at 03:33
  • 1
    Why are you trying to write to Copy.exe if Copy.exe is the executable that you are running? Of course the file will be busy - you're running it right then. – deyur Aug 27 '15 at 03:34
  • Is it possible to make it editable so as to bypass that exception?? the reason am asking this is because that particular exception terminate the whole program(some missing classes on the code above) which there was no need to add them – Denis Aug 27 '15 at 03:37
  • What are you trying to accomplish here? As a first step, you probably want to change the `to` file to point to something else (either something that doesn't exist - which you can create - or something that isn't busy). – deyur Aug 27 '15 at 03:44
  • To answer your question about bypassing that exception: Your program is crashing because `os` is null. So if you want the program to continue on anyway, wrap every usage of `os` (after attempting to assign it a value) with a check like `if (os != null)`. – deyur Aug 27 '15 at 20:29

0 Answers0