无法在Windows上删除java中的文件...?

时间:2022-04-21 16:41:29

I am trying to use file.delete() but it does not delete file ? I tested it on linux it delete files but on windows it does not delete file why..?

我试图使用file.delete()但它不删除文件?我在linux上测试它删除文件,但在Windows上它不删除文件为什么..?

code :

private File getFiletobeUpload(File foto) {

    boolean errorRename = true;
    File uploadFile = null;
    File testFile = foto;
    String subdirname = this.checkDir(foto);

    if (testFile.canWrite()) {
        uploadFile = new File(AppConstants.PHOTOPATH + "/" + subdirname +  "/" + testFile.getName());


        try {
            FileInputStream origStream = new FileInputStream(testFile);
            FileOutputStream outStream = new FileOutputStream(uploadFile);
            origStream.getChannel().transferTo(0, testFile.length(), outStream.getChannel());
            origStream.close();
            origStream = null;
            outStream.close();
            outStream = null;

        } catch (Exception e) {
            this.errorString += "error while writing to orig dir";
            logger.error(e);
        }

        errorRename = !testFile.delete();
        if (errorRename) {
            this.errorString += "error while deleting the file";
        }
    }

    testFile = null;
    return uploadFile;
}

3 个解决方案

#1


your code seems a bit odd, you are checking "testFile" for write access but then actually reading from it (FileInputStream). Is the first try/catch block running without exception? Maybe you should check if the file even exists:

您的代码看起来有点奇怪,您正在检查“testFile”以进行写访问,但实际上是从它读取(FileInputStream)。第一个try / catch块是否正常运行?也许您应该检查文件是否存在:

System.out.println("File exists: "+testFile.exists());
errorRename = !testFile.delete();

Also if you just want to rename a file, use:

此外,如果您只想重命名文件,请使用:

 file.renameTo(File dest)

#2


A general suggestion: consider using the classes in the java.nio package to your file IO if you can (not sure which version of Java you're running with) because the error handling is improved and you should be able to find a more specific reason for the failure based on the type of exception thrown and the exception message.

一般建议:考虑使用java.nio包中的类到您的文件IO(如果可以的话,不确定您运行的是哪个版本的Java),因为错误处理得到了改进,您应该能够找到更具体的基于抛出的异常类型和异常消息导致失败的原因。

#3


As per API, the disk-drive specifier - "/" for the UNIX root directory, and "\\" for a Microsoft Windows UNC pathname.

根据API,磁盘驱动器说明符 - UNIX根目录的“/”和Microsoft Windows UNC路径名的“\\”。

so for windows:

所以对于Windows:

uploadFile = new File(AppConstants.PHOTOPATH + "\\" + subdirname +  "\\" + testFile.getName());

#1


your code seems a bit odd, you are checking "testFile" for write access but then actually reading from it (FileInputStream). Is the first try/catch block running without exception? Maybe you should check if the file even exists:

您的代码看起来有点奇怪,您正在检查“testFile”以进行写访问,但实际上是从它读取(FileInputStream)。第一个try / catch块是否正常运行?也许您应该检查文件是否存在:

System.out.println("File exists: "+testFile.exists());
errorRename = !testFile.delete();

Also if you just want to rename a file, use:

此外,如果您只想重命名文件,请使用:

 file.renameTo(File dest)

#2


A general suggestion: consider using the classes in the java.nio package to your file IO if you can (not sure which version of Java you're running with) because the error handling is improved and you should be able to find a more specific reason for the failure based on the type of exception thrown and the exception message.

一般建议:考虑使用java.nio包中的类到您的文件IO(如果可以的话,不确定您运行的是哪个版本的Java),因为错误处理得到了改进,您应该能够找到更具体的基于抛出的异常类型和异常消息导致失败的原因。

#3


As per API, the disk-drive specifier - "/" for the UNIX root directory, and "\\" for a Microsoft Windows UNC pathname.

根据API,磁盘驱动器说明符 - UNIX根目录的“/”和Microsoft Windows UNC路径名的“\\”。

so for windows:

所以对于Windows:

uploadFile = new File(AppConstants.PHOTOPATH + "\\" + subdirname +  "\\" + testFile.getName());