2013-12-19 95 views
0

我正在嘗試編寫一個將代碼從源文件複製到目標文件的函數。java.io.FileNotFoundException--未找到文件

copyCode("D:/rraina_IN-L0124_173"+fileName.substring(1), oldTempFile);

這是我的函數調用。

String oldTempFile = "D:/rraina_IN-L0124_173/temp/Old_" + fileName;

這是oldTempFile這是目標。

這是函數。

private static void copyCode(String src, String destination) throws IOException { 
     FileChannel src1 = new FileInputStream(new File(src)).getChannel(); 
     FileChannel dest1 = new FileOutputStream(new File(destination)).getChannel(); 
     dest1.transferFrom(src1, 0, src1.size()); 
     src1.close(); 
     dest1.close(); 
    } 

然而,當我運行它,我得到的錯誤:

爲失敗 文件:/gatherer/gather/main/scripts/HartfordRetirement.javajava.io.FileNotFoundException: d:\ rraina_IN -L0124_173 \ TEMP \ Old_HartfordRetirement.java(系統 找不到指定的路徑)

+1

檢查名稱爲Old_HartfordRetirement.java的文件存在於目錄D:\ rraina_IN-L0124_173 \ temp \ ??中檢查文件名稱的拼寫 – AJJ

回答

1

檢查該文件的存在,

File file = new File(destination); 
boolean isFileExists = file.exists(); 
System.out.println(isFileExists); // this should return true if the file is present 

檢查與名稱Old_HartfordRetirement.java的文件出現在目錄d:\ rraina_IN-L0124_173 \文件名的拼寫TEMP \檢查

0

你應該檢查你想使用的文件夾中的所有腦幹他們和源文件。 使用此代碼:

private static void copyCode(String src, String destination) throws IOException { 
    File srcFile = new File(src); 

    if (srcFile.exist()) { 
     File destFile = new File(destination); 
     File destFileParent = destFile.getParentFile(); 
     if (!destFileParent.exist()) destFileParent.mkdirs(); 

     FileChannel src1 = new FileInputStream(srcFile).getChannel(); 
     FileChannel dest1 = new FileOutputStream(destFile)).getChannel(); 
     dest1.transferFrom(src1, 0, src1.size()); 
     src1.close(); 
     dest1.close(); 
    } 
} 
0

在你的方法調用,源參數丟失一個文件分隔符。

copyCode("D:/rraina_IN-L0124_173/"+fileName.substring(1), oldTempFile); 

此外,我會建議檢查fileName.substring(1)的值是否返回正確的文件名。