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))來輸入這個東西,但沒有從這種方法的幫助。
對於這樣簡單的任務一樣,我會建議使用現有的東西,如番石榴'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