0

I am writting a code for copying a file from one location to another.Code for copying this file is perfect,only thing is while copying a file from one location to another.I am getting this exception

java.io.FileNotFoundException: /Users/Shared/Jenkins/see/rundata/8889/63.PNG (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)

Actually this file generates at runtime and once the code execution is done then this file will not be there.So,I checked it manually while debugging the application and I found that this .png file was there.

public static void copyFile(File sourceFile, File destFile) {
        // http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java

        try {
            if (!destFile.exists()) {
                destFile.createNewFile();
            }

            FileChannel source = null;
            FileChannel destination = null;

            try {
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                destination.transferFrom(source, 0, source.size());
            } finally {
                if (source != null) {
                    source.close();
                }
                if (destination != null) {
                    destination.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

More Info for this Question:-

Actually my application takes screenshot for the each and every screen on my mobile application and puts it in the system and from there i am copying these images in the project by using this method.So,while debugging this method I found that it is able to copy this image file but when I switching off the debug mode image file is not getting copied and I am started getting this no such directory found exception.

So,I thought that it could be something related with sleep time i tried to enter that thing as well by (Thread.sleep(30000)),but no help from this approach.

Manish Singh
  • 310
  • 1
  • 5
  • 19
  • For such easy task like that I would recommend using existing stuff like Guava `Files` class http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html#copy(java.io.File,%20java.io.File). No need to write own methods that might be buggy. – Tom Sep 23 '14 at 20:58

1 Answers1

0

There is a requirement of extra milliseconds. Because image was getting captured from mobile device every second and was put into that particular location.In debug mode since it was able to get more time,so image was getting copied into the desired location but in non debug mode time to copy image was few fraction of seconds.

so by providing

Thread.sleep(6000)

solves the problem

Manish Singh
  • 310
  • 1
  • 5
  • 19