java.io.FileNotFoundException--找不到文件

时间:2013-12-19 07:22:35

标签: java

我正在尝试编写一个将代码从源文件复制到目标文件的函数。

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(系统   找不到指定的路径)

3 个答案:

答案 0 :(得分: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 \检查文件名拼写

答案 1 :(得分: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();
    }
}

答案 2 :(得分:0)

在方法调用中,source参数缺少一个文件分隔符。

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

另外我建议检查fileName.substring(1)的值是否返回正确的文件名。

相关问题