检测何时由于名称中的错误字符而无法创建文件
谁能告诉我如何处理java中的非法文件名?当我在 Windows 上运行以下命令时:
File badname = new File("C:\\Temp\\a:b");
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
FileWriter w = new FileWriter(badname);
w.write("hello world");
w.close();
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
输出显示文件已创建并具有预期的长度,但在 C:\Temp 中我只能看到一个名为“a”、长度为 0 的文件。 java把文件放在哪里?
我正在寻找的是一种在无法创建文件时抛出错误的可靠方法。我不能使用 contains() 或 length() - 还有什么其他选项?
Can anyone tell me how to cope with illegal file names in java? When I run the following on Windows:
File badname = new File("C:\\Temp\\a:b");
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
FileWriter w = new FileWriter(badname);
w.write("hello world");
w.close();
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
The output shows that the file has been created and has the expected length, but in C:\Temp all I can see is a file called "a" with 0 length. Where is java putting the file?
What I'm looking for is a reliable way to throw an error when the file can't be created. I can't use exists() or length() - what other options are there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在该特定示例中,数据被写入 命名流。您可以按如下方式查看从命令行写入的数据:
有关有效文件名的信息,看这里。
要回答您的具体问题:exists() 应该足够了。即使在这种情况下,毕竟数据已被写入指定位置 - 它只是不是您期望的位置!如果您认为这种情况会给您的用户带来问题,请检查文件名中是否存在冒号。
In that particular example, the data is being written to a named stream. You can see the data you've written from the command line as follows:
For information about valid file names, look here.
To answer your specific question: exists() should be sufficient. Even in this case, after all, the data is being written to the designated location - it just wasn't where you expected it to be! If you think this case will cause problems for your users, check for the presence of a colon in the file name.
我建议查看正则表达式。它们允许您分解字符串并查看某些特征是否适用。另一种可行的方法是将字符串拆分为 char[],然后处理每个点以查看其中的内容以及它是否合法......但我认为 RegEx 会工作得更好。
I would suggest looking at Regular Expressions. They allow you to break apart a string and see if certain characteristics apply. The other method that would work is splitting the String into a char[], and then processing each point to see what's in it, and if it's legal... but I think RegEx would work much better.
您应该查看 正则表达式 并创建一个与任何非法字符,如下所示:
注意:我尚未测试此代码,但它应该足以让您走上正轨。上面的代码将匹配任何包含
:
,;
, `!' 的字符串和 '?'。请随意添加/删除您认为合适的内容。You should take a look at Regular Expressions and create a pattern which will match any illegal character, something like this:
Note: I have not tested this code, but it should be enough to get you on the right track. The above code will match any string which contains a
:
,;
, `!' and '?'. Feel free to add/remove as you see fit.您可以使用
File.renameTo(File dest);
。You can use
File.renameTo(File dest);
.首先获取文件名:
创建一个包含文件名中不允许的所有特殊字符的数组。
对于数组中的每个
char
,检查fileName
是否包含它。我猜想,Java 有一个预先构建的 API。检查这个。
注意:此解决方案假设父级目录存在
Get the file name first:
Create an array of all special chars not allowed in file names.
for each
char
in array, check iffileName
contains it. I guess, Java has a pre-built API for it.Check this.
Note: This solution assumes that parent directory exists