我正在尝试创建 GUI 示例,当用户单击按钮时删除文件和/或目录,但我看到文件被永久删除,如何使其移动到回收站而不是这个
if (File_path.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Please select a file or directory", "Info", JOptionPane.INFORMATION_MESSAGE);
} else {
File FileName = new File(File_path.getText());
boolean FileDeleted = FileName.delete();
if (FileDeleted == true) {
JOptionPane.showMessageDialog(null, "File Deleted Successfully", "Info", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "File Not Found", "Info", JOptionPane.INFORMATION_MESSAGE);
}
}
实际上,这是一个被触发但被忽视的错误,因为开发人员相信它won't be cross-platform-compatible
如果添加移动到回收站功能。你可以阅读相关内容here http://www.ryanlowe.ca/blog/archives/000574_java_delete_to_recycle_bin.php
using C++: 但你可以这样做External APIs
。在...的帮助下JNI
调用 WindowsSHFileOperation
API,设置FO_DELETE
标志在SHFILEOPSTRUCT
结构。
这是参考 http://www.jroller.com/ethdsy/entry/send_to_recycle_bin
using JAVA :Use [com.sun.jna.platform.win32.W32FileUtils
], 其中有moveToTrash
and hasTrash
定义的方法。
另一种方法是使用com.sun.jna.platform.FileUtils;
示例代码:
import java.io.File;
import java.io.IOException;
import com.sun.jna.platform.FileUtils;
public class MoveToTrash {
public static void main(String[] args){
FileUtils fileUtils = FileUtils.getInstance();
if (fileUtils.hasTrash()) {
try {
fileUtils.moveToTrash( new File[] {new File("c:/folder/abcd.txt") });
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
else {
System.out.println("No Trash available");
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)