Hãy xem xét ví dụ sau lớp Java (pom.xml dưới đây):hành vi Odd khi xóa tập tin với Files.delete()
package test.filedelete;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import org.apache.commons.io.IOUtils;
public class Main
{
public static void main(String[] args) throws IOException
{
byte[] bytes = "testtesttesttesttesttesttesttesttesttest".getBytes();
InputStream is = new ByteArrayInputStream(bytes);
Path tempFileToBeDeleted = Files.createTempFile("test", "");
OutputStream os = Files.newOutputStream(tempFileToBeDeleted);
IOUtils.copy(is, os);
deleteAndCheck(tempFileToBeDeleted);
// breakpoint 1
System.out.println("\nClosing stream\n");
os.close();
deleteAndCheck(tempFileToBeDeleted);
}
private static void deleteAndCheck(Path file) throws IOException
{
System.out.println("Deleting file: " + file);
try
{
Files.delete(file);
}
catch (NoSuchFileException e)
{
System.out.println("No such file");
}
System.out.println("File really deleted: " + !Files.exists(file));
System.out.println("Recreating deleted file ...");
try
{
Files.createFile(file);
System.out.println("Recreation successful");
}
catch (IOException e)
{
System.out.println("Recreation not possible, exception: " + e.getClass().getName());
}
}
}
Tôi viết thư cho một FileOutputStream và cố gắng xóa các tập tin sau đó mà không đóng cửa Luồng đầu tiên. Đây là vấn đề ban đầu của tôi, và tất nhiên là sai, nhưng nó dẫn đến một số quan sát kỳ lạ.
Khi bạn chạy các phương pháp chính trên Windows 7 nó tạo ra kết quả như sau:
Deleting file: C:\Users\MSCHAE~1\AppData\Local\Temp\test6100073603559201768
File really deleted: true
Recreating deleted file ...
Recreation not possible, exception: java.nio.file.AccessDeniedException
Closing stream
Deleting file: C:\Users\MSCHAE~1\AppData\Local\Temp\test6100073603559201768
No such file
File really deleted: true
Recreating deleted file ...
Recreation successful
- Tại sao cuộc gọi đầu tiên để Files.delete() không ném một ngoại lệ?
- Tại sao cuộc gọi sau đây đến Files.exist() trả về false?
- Tại sao không thể tạo tệp mới?
Về câu hỏi cuối cùng tôi nhận thấy rằng tệp vẫn hiển thị trong Explorer khi bạn dừng tại điểm ngắt 1. Khi bạn chấm dứt JVM, tệp sẽ bị xóa. Sau khi đóng luồng deleteAndCheck() hoạt động như mong đợi.
Dường như với tôi rằng việc xóa không được truyền sang HĐH trước khi đóng luồng và API tệp không phản ánh đúng cách.
Ai đó có thể giải thích chính xác những gì đang xảy ra ở đây?
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>filedelete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
Cập nhật để làm rõ
Các tập tin sẽ biến mất trong Windows explorer, nếu dòng được đóng lại và Files.delete() được gọi - hoạt động cuối cùng trình kích hoạt - hoặc nếu Files.delete() đã được gọi mà không đóng luồng và JVM bị chấm dứt.
Hệ điều hành là gì? Nếu giống Unix, bạn có thể thử và in các quyền của thư mục mẹ không? – fge
Và bằng cách này, bạn có thể trực tiếp sử dụng 'Files.copy()' để sao chép nội dung của 'InputStream' vào một' Path'; không cần IOUtils – fge
Thử đặt điểm ngắt trên 'Files.delete (file);' và sau đó bước qua nó. Tệp có thực sự biến mất khỏi cửa sổ trình khám phá tại thời điểm đó không? Tôi nghi ngờ rằng bằng cách nào đó xóa thành công, nhưng cửa sổ đang giữ tập tin vì có một tham chiếu khác mở cho nó. Điều này có thể là một cái gì đó thấp cấp trong cách cửa sổ hoạt động. Tôi sẽ không mong đợi như vậy trên một hệ thống Posix (ví dụ: Linux). –