java系统找不到指定的文件

jam*_*ame 3 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试代码

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

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

  1. 文件'temp.txt'存在.
  2. 路径没问题.

我猜这是权限问题.谁能想出答案谢谢!

Ern*_*ill 6

我们需要getFileName()确定方法,但基于错误消息和方法名称,我怀疑问题只是这个方法只返回文件的名称,删除路径信息,所以文件确实是, 未找到.