首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文件not -文件未找到

文件not -文件未找到
EN

Stack Overflow用户
提问于 2013-12-19 07:22:35
回答 3查看 471关注 0票数 0

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

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

这是我的函数呼叫。

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

这是oldTempFile,它是目的地。

这就是功能。

代码语言:javascript
复制
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();
    }

然而,当我运行它时,我会得到错误:

file:/gatherer/gather/main/scripts/HartfordRetirement.javajava.io.FileNotFoundException: D:\rraina_IN-L0124_173\temp\Old_HartfordRetirement.java失败(系统找不到指定的路径)

EN

回答 3

Stack Overflow用户

发布于 2013-12-19 07:29:03

请检查文件是否存在,

代码语言:javascript
复制
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 -L 0124_173\temp\检查文件名的拼写

票数 1
EN

Stack Overflow用户

发布于 2013-12-19 08:18:39

您应该检查要处理的文件夹和源文件是否存在。使用以下代码:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-12-19 09:26:06

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

代码语言:javascript
复制
copyCode("D:/rraina_IN-L0124_173/"+fileName.substring(1), oldTempFile);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20675658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档