2014-09-23 44 views
0

我正在写一个代码,用于将文件从一个位置复制到另一个位置。用于复制此文件的代码是完美的,只有当将文件从一个位置复制到另一个位置时。我正在此例外java中的文件复制问题[没有这样的文件或目录]

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) 

其实这个文件生成在运行时,一旦代码执行完成那么这个文件不会there.So,我手动检查它在调试应用程序,我发现这一点。 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))来输入这个东西,但没有从这种方法的帮助。

+0

对于这样简单的任务一样,我会建议使用现有的东西,如番石榴'Files'类HTTP://docs.guava-libraries.googlecode。 COM/GIT中/的Javadoc/COM /谷歌/普通/ IO/Files.html#拷贝(java.io.File的,%20java.io.File)。无需编写自己的方法,可能是越野车。 – Tom 2014-09-23 20:58:23

回答

0

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

所以通过提供

Thread.sleep(6000) 

解决问题

相关问题