Java'file.delete()'不删除指定的文件 [英] Java 'file.delete()' Is not Deleting Specified File

查看:833
本文介绍了Java'file.delete()'不删除指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是目前我必须删除文件,但它不工作。我认为这可能是许可问题,但事实并非如此。我测试的文件是空的,存在,所以不知道为什么不删除它。

This is currently what I have to delete the file but it's not working. I thought it may be permission problems or something but it wasn't. The file that I am testing with is empty and exists, so not sure why it doesn't delete it.

UserInput.prompt("Enter name of file to delete");
String name = UserInput.readString();
File file = new File("\\Files\\" + name + ".txt");
file.delete();

任何帮助将非常感激!

我现在有:

File file = new File(catName + ".txt");
String path = file.getCanonicalPath();
File filePath = new File(path);
filePath.delete();

在运行时尝试找到正确的路径,以便如果将程序传输到其他计算机它仍然会找到该文件。

To try and find the correct path at run time so that if the program is transferred to a different computer it will still find the file.

推荐答案

确保找出您当前的工作目录,并相对于其编写文件路径。

Be sure to find out your current working directory, and write your filepath relative to it.

此代码:

File here = new File(".");
System.out.println(here.getAbsolutePath());

...将打印出该目录。

... will print out that directory.

此外,与您的问题无关,尝试使用 File.separator 保持与操作系统无关。反斜杠仅在Windows上工作。

Also, unrelated to your question, try to use File.separator to remain OS-independent. Backslashes work only on Windows.

这篇关于Java'file.delete()'不删除指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆