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

查看:47
本文介绍了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天全站免登陆