Java system cannot find the specified file

I used Java to copy the file, but an exception appeared in it (the system cannot find the specified file).

Codes

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

Test codes

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

the exception is java.io.FileNotFoundException: temp.txt(the system can not find the file specified)

  • The temp.txt file is an existence.
  • The way is right, no problem.

I assume this is a permission issue. who can come up with an answer thanks!

+3
source share
1 answer

We have to make sure that the method getFileName(), but based on the error message and the method name, I suspect that the problem is only that this method returns only the file name, deleting the path information, so that the file really was not found.

+6
source

All Articles