java系统找不到指定的文件

时间:2012-04-16 03:17:58

标签: java

我使用Java来复制文件,但它出现异常(系统找不到指定的文件)。

代码是

public static void copyFile(String sourceFile, String destFile){
    try {       
        InputStream in = new FileInputStream(sourceFile);
        OutputStream os = new FileOutputStream(destFile);
        byte[] buffer = new byte[1024];
        int count;
        while ((count = in.read(buffer)) > 0) {
            os.write(buffer, 0, count);
        }
        in.close();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

测试代码

public static void main(String[] args) {
    String name = getFileName("D:/z/temp.txt");
    String target = "D:/tem.txt";
    copyFile(name, target);
}

例外是java.io.FileNotFoundException: temp.txt(the system can not find the file specified)

  1. 文件'temp.txt'存在。
  2. 路径没问题。
  3. 我猜这是权限问题。谁能想出答案谢谢!

1 个答案:

答案 0 :(得分:6)

我们需要确定方法getFileName(),但根据错误消息和方法名称,我怀疑问题只是该方法只返回文件名,删除了路径信息,确实找不到文件。

相关问题