简体   繁体   English

java.io.FileNotFoundException-找不到文件

[英]java.io.FileNotFoundException— File not found

I am trying to write a function that would copy code from a source file to destination file. 我正在尝试编写一个将代码从源文件复制到目标文件的函数。

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

This is my function call. 这是我的函数调用。

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

this is oldTempFile that is the destination. 这是作为目的地的oldTempFile

This is the function. 这就是功能。

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

However when I run it I get the error : 但是,当我运行它时,我得到了错误:

Failed for file:/gatherer/gather/main/scripts/HartfordRetirement.javajava.io.FileNotFoundException: D:\\rraina_IN-L0124_173\\temp\\Old_HartfordRetirement.java (The system cannot find the path specified) 文件失败:/gatherer/gather/main/scripts/HartfordRetirement.javajava.io.FileNotFoundException:D:\\ rraina_IN-L0124_173 \\ temp \\ Old_HartfordRetirement.java(系统找不到指定的路径)

Check for present of the file as, 检查文件是否存在,

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

check for the file with name Old_HartfordRetirement.java is present in directory D:\\rraina_IN-L0124_173\\temp\\ check for spell of file name 检查目录D:\\ rraina_IN-L0124_173 \\ temp \\中是否存在名称为Old_HartfordRetirement.java的文件。检查文件名的拼写

You should check existance of folders that you want to work with them and source file. 您应该检查要使用的文件夹和源文件的存在。 use this code: 使用此代码:

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

In your method invocation, the source argument is missing one file seperator. 在您的方法调用中,source参数缺少一个文件分隔符。

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

Also I would suggest to check that the value of fileName.substring(1) is returning the correct file name. 我也建议检查一下fileName.substring(1)的值是否返回正确的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM