删除中间有空格的文件名

时间:2022-07-18 13:21:49

i read on several posts that we for deleting a file through java which has spaces in the name, i can use delete() method (Java 6). eg:

我在几个帖子上读到我们通过java删除文件,名称中有空格,我可以使用delete()方法(Java 6)。例如:

 File f = new File("/mnt/test ex.txt");  
 f.delete();  

but when I'm making a file object like this () :

但是当我正在制作像这样的文件对象时():

 StringBuilder fullFileName = "C:/Temp_Folder\week month.xlsx";
 fileToRead = new File(fullFileName.toString());
 fileToRead.delete();

I'm not able to do so and i get the following exception :

我无法这样做,我得到以下异常:

java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx" (The filename, directory name, or volume label syntax is incorrect) What am i missing here?

java.io.FileNotFoundException:“C:\ Temp_Folder \ week month.xlsx”(文件名,目录名或卷标语法不正确)我在这里缺少什么?

P.s. : I tried using quotes on the filename as well without success

附: :我尝试在文件名上使用引号也没有成功

  fileToRead = new File('"'+fullFileName.toString()+'"');

Edit : I've edited the quotes on the stringBuilder (a type from my end). Actually the StringBuilder object is a parameter and we are appending objects to fetch the actual name. I just gave you the final declaration. As far as week month.xlsx goes, that is the name of the file and not two different variables (which means the filename DOES have spaces in between; it could be something like

编辑:我已经编辑了stringBuilder上的引号(我的一个类型)。实际上StringBuilder对象是一个参数,我们附加对象来获取实际名称。我刚刚给你最后的声明。至于周月.xlsx,这是文件的名称,而不是两个不同的变量(这意味着文件名之间有空格;它可能是这样的

Name with spaces.xlsx

使用spaces.xlsx命名

Thanks for the quick turnaround everyone.

感谢大家的快速周转。

2 个解决方案

#1


1  

You do NOT need a specific treatment for file names with spaces in Java -- or any other programming language with a file access API for that matter.

对于带有Java空格的文件名,或者任何其他带有文件访问API的编程语言,您不需要特定的处理方法。

Do not mix Java with a command interpreter.

不要将Java与命令解释器混合使用。

In your case, your File should be declared as:

在您的情况下,您的文件应声明为:

new File("C:\\Temp_Folder\\name with spaces.xlsx")

and that's it.

就是这样。

If Java reports a FileNotFoundException then there is a problem. Unfortunately, the File API is broken and this exception can be thrown if the file exists but you cannot read it, for instance. Have a look at the complete stack trace.

如果Java报告FileNotFoundException,则存在问题。遗憾的是,文件API已损坏,如果文件存在但您无法读取该文件,则会抛出此异常。看看完整的堆栈跟踪。

Do yourself a favour: use Java 7 and the new Files API. With this API, exceptions actually make some sense -- and a delete operation will not "silently" fail either.

帮自己一个忙:使用Java 7和新的Files API。使用此API,异常实际上是有道理的 - 并且删除操作也不会“默默地”失败。

As to building the filename itself, you can for example use String.format():

至于构建文件名本身,您可以使用String.format():

final String filename = String.format("C:\\Temp_Folder\\%s %s.xlsx", month, week);

#2


1  

According to the exception:

根据例外情况:

java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx"

You are looking for the following file:

您正在寻找以下文件:

"C:\Temp_Folder\week month.xlsx"

Note the quotes! This file does not exist.

注意报价!该文件不存在。

You will have to modify your code to ensure that your file name does not include the surrounding quotes (not needed).

您必须修改代码以确保您的文件名不包含周围的引号(不需要)。

I.e. (Assuming java 6 here)

即(假设java 6在这里)

File file = new File("C:\\Temp_Folder\\week month.xlsx");
file.delete();

Note, the backslash is an escape character hence it is doubled in the string.

注意,反斜杠是一个转义字符,因此它在字符串中加倍。

#1


1  

You do NOT need a specific treatment for file names with spaces in Java -- or any other programming language with a file access API for that matter.

对于带有Java空格的文件名,或者任何其他带有文件访问API的编程语言,您不需要特定的处理方法。

Do not mix Java with a command interpreter.

不要将Java与命令解释器混合使用。

In your case, your File should be declared as:

在您的情况下,您的文件应声明为:

new File("C:\\Temp_Folder\\name with spaces.xlsx")

and that's it.

就是这样。

If Java reports a FileNotFoundException then there is a problem. Unfortunately, the File API is broken and this exception can be thrown if the file exists but you cannot read it, for instance. Have a look at the complete stack trace.

如果Java报告FileNotFoundException,则存在问题。遗憾的是,文件API已损坏,如果文件存在但您无法读取该文件,则会抛出此异常。看看完整的堆栈跟踪。

Do yourself a favour: use Java 7 and the new Files API. With this API, exceptions actually make some sense -- and a delete operation will not "silently" fail either.

帮自己一个忙:使用Java 7和新的Files API。使用此API,异常实际上是有道理的 - 并且删除操作也不会“默默地”失败。

As to building the filename itself, you can for example use String.format():

至于构建文件名本身,您可以使用String.format():

final String filename = String.format("C:\\Temp_Folder\\%s %s.xlsx", month, week);

#2


1  

According to the exception:

根据例外情况:

java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx"

You are looking for the following file:

您正在寻找以下文件:

"C:\Temp_Folder\week month.xlsx"

Note the quotes! This file does not exist.

注意报价!该文件不存在。

You will have to modify your code to ensure that your file name does not include the surrounding quotes (not needed).

您必须修改代码以确保您的文件名不包含周围的引号(不需要)。

I.e. (Assuming java 6 here)

即(假设java 6在这里)

File file = new File("C:\\Temp_Folder\\week month.xlsx");
file.delete();

Note, the backslash is an escape character hence it is doubled in the string.

注意,反斜杠是一个转义字符,因此它在字符串中加倍。