java中的文件复制问题[无此类文件或目录]

时间:2014-09-23 20:45:36

标签: java file-io

我正在编写一个代码,用于将文件从一个位置复制到另一个位置。用于复制此文件的代码是完美的,唯一的办法是将文件从一个位置复制到另一个位置。我收到此异常

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)

实际上这个文件在运行时生成,一旦代码执行完毕,那么这个文件就不存在了。所以,我在调试应用程序时手动检查了它,我发现这个.png文件就在那里。

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

此问题的更多信息: -

实际上我的应用程序为我的移动应用程序上的每个屏幕截取屏幕截图并将其放入系统中,然后我使用此方法在项目中复制这些图像。所以,在调试此方法时,我发现它能够复制此图像文件,但当我关闭调试模式图像文件没有被复制,我开始得到这个没有这样的目录发现异常。

所以,我认为它可能与睡眠时间有关,我试图通过(Thread.sleep(30000))进入那个东西,但是这种方法没有帮助。

1 个答案:

答案 0 :(得分:0)

需要额外的毫秒数。因为图像每秒都从移动设备捕获并被放入该特定位置。在调试模式下,因为它能够获得更多时间,所以图像被复制到所需位置但在非调试模式下复制图像的时间很少几分之一。

所以提供

Thread.sleep(6000)

解决问题