使用Java中的delete()方法删除文件

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

I have a little doubt about following code:

我对以下代码有点怀疑:

try {
    File file = new File("writing");
    file.createNewFile();
    System.out.println(file.delete());
    System.out.println(file.exists());

    PrintWriter pw = new PrintWriter(file);
    pw.print(324.2342);
    pw.flush();
    pw.close();
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    System.out.println(br.readLine());
    br.close();
} catch(IOException ioE) {
    System.out.println("Indeed");
}

Why in this circumstance the method file.delete() apparently says that it works as it returns "true" when executed and it gets also confirmed by the file.exists() method which returns "false". However at runtime I do not get any exception like "IOException the file "writing" does not exist" or something likewise.

为什么在这种情况下,方法file.delete()显然表示它的工作原理是它在执行时返回“true”,并且它也被file.exists()方法确认,返回“false”。但是在运行时我没有得到任何异常,例如“IOException the file”写“不存在”或类似的东西。

Why does the file keep staying in the heap even though deleted physically? Shouldn't it be automatically garbage collected as soon as the delete method gets called? I know it does not because I saw the output.

为什么即使在物理上删除文件仍保留在堆中?一旦调用delete方法,它不应该自动被垃圾收集吗?我知道它不是因为我看到了输出。

4 个解决方案

#1


4  

This is because the File represents a abstract path, see the JavaDoc for it http://docs.oracle.com/javase/6/docs/api/java/io/File.html. It does not represent a file handle in the OS.
The line in your code:

这是因为File表示一个抽象路径,请参阅JavaDoc http://docs.oracle.com/javase/6/docs/api/java/io/File.html。它不代表OS中的文件句柄。代码中的行:

PrintWriter pw = new PrintWriter(file);

Simply creates a new file. Try deleting the file after calling this...

只需创建一个新文件。尝试在调用此文件后删除该文件...

#2


2  

File object represents a path to a physical file on the file system either exists or not. That's why you have exists() (to check if it exists) and createNewFile() (to create the file if it is not found).

File对象表示文件系统上的物理文件的路径是否存在。这就是为什么你有exists()(检查它是否存在)和createNewFile()(如果找不到则创建文件)。


Also note that PrintWriter(File file) creates a new file if it does not exist.

另请注意,PrintWriter(文件文件)会创建一个新文件(如果它不存在)。

Parameters:

file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

file - 要用作此writer的目标的文件。如果该文件存在,那么它将被截断为零大小;否则,将创建一个新文件。输出将写入文件并进行缓冲。

#3


1  

The File is handle to real file (that exists or not). You create and then delete the file above, as you say - all good so far.

文件是真实文件的句柄(是否存在)。你正在创建并删除上面的文件 - 到目前为止一切都很好。

When you come to the PrintWriter later on it creates the file once more when you use it - it doesnt matter that you deleted it before.

当您稍后来到PrintWriter时,它会在您使用它时再次创建该文件 - 之前删除它并不重要。

In fact depending on your use case this might be exaclty wht you want - you may want to delete an old log file for example before re-createing and writing to it once more.

实际上,根据您的使用情况,这可能是您想要的 - 例如,您可能希望删除旧的日志文件,例如在重新创建并再次写入之前。

Finally, nothing in your code will be eligible for garbage collection until your method exist, and even then the underyling file will continue to exist (if you dont delete it agin) - and any garbage colleciton in this case wouldnt effect the underlying file. It'll be deleted after the delete invokation and exist again once youre PrintWriter is done with it.

最后,在你的方法存在之前,你的代码中的任何内容都没有资格进行垃圾收集,即使这样,底层文件也将继续存在(如果你不删除它) - 并且在这种情况下任何垃圾收集都不会影响底层文件。它将在删除调用后删除,并在PrintWriter完成后再次存在。

Hope this helps!

希望这可以帮助!

#4


0  

The file doesn't have a link to a particular file, rather to any file pointer by the file's path. With this line you're creating a new file:

该文件没有指向特定文件的链接,而是指向文件路径的任何文件指针。使用此行,您将创建一个新文件:

PrintWriter pw = new PrintWriter(file);

#1


4  

This is because the File represents a abstract path, see the JavaDoc for it http://docs.oracle.com/javase/6/docs/api/java/io/File.html. It does not represent a file handle in the OS.
The line in your code:

这是因为File表示一个抽象路径,请参阅JavaDoc http://docs.oracle.com/javase/6/docs/api/java/io/File.html。它不代表OS中的文件句柄。代码中的行:

PrintWriter pw = new PrintWriter(file);

Simply creates a new file. Try deleting the file after calling this...

只需创建一个新文件。尝试在调用此文件后删除该文件...

#2


2  

File object represents a path to a physical file on the file system either exists or not. That's why you have exists() (to check if it exists) and createNewFile() (to create the file if it is not found).

File对象表示文件系统上的物理文件的路径是否存在。这就是为什么你有exists()(检查它是否存在)和createNewFile()(如果找不到则创建文件)。


Also note that PrintWriter(File file) creates a new file if it does not exist.

另请注意,PrintWriter(文件文件)会创建一个新文件(如果它不存在)。

Parameters:

file - The file to use as the destination of this writer. If the file exists then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

file - 要用作此writer的目标的文件。如果该文件存在,那么它将被截断为零大小;否则,将创建一个新文件。输出将写入文件并进行缓冲。

#3


1  

The File is handle to real file (that exists or not). You create and then delete the file above, as you say - all good so far.

文件是真实文件的句柄(是否存在)。你正在创建并删除上面的文件 - 到目前为止一切都很好。

When you come to the PrintWriter later on it creates the file once more when you use it - it doesnt matter that you deleted it before.

当您稍后来到PrintWriter时,它会在您使用它时再次创建该文件 - 之前删除它并不重要。

In fact depending on your use case this might be exaclty wht you want - you may want to delete an old log file for example before re-createing and writing to it once more.

实际上,根据您的使用情况,这可能是您想要的 - 例如,您可能希望删除旧的日志文件,例如在重新创建并再次写入之前。

Finally, nothing in your code will be eligible for garbage collection until your method exist, and even then the underyling file will continue to exist (if you dont delete it agin) - and any garbage colleciton in this case wouldnt effect the underlying file. It'll be deleted after the delete invokation and exist again once youre PrintWriter is done with it.

最后,在你的方法存在之前,你的代码中的任何内容都没有资格进行垃圾收集,即使这样,底层文件也将继续存在(如果你不删除它) - 并且在这种情况下任何垃圾收集都不会影响底层文件。它将在删除调用后删除,并在PrintWriter完成后再次存在。

Hope this helps!

希望这可以帮助!

#4


0  

The file doesn't have a link to a particular file, rather to any file pointer by the file's path. With this line you're creating a new file:

该文件没有指向特定文件的链接,而是指向文件路径的任何文件指针。使用此行,您将创建一个新文件:

PrintWriter pw = new PrintWriter(file);