检测何时由于名称中的错误字符而无法创建文件

发布于 2025-01-03 08:12:44 字数 495 浏览 2 评论 0原文

谁能告诉我如何处理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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本王不退位尔等都是臣 2025-01-10 08:12:44

在该特定示例中,数据被写入 命名流。您可以按如下方式查看从命令行写入的数据:

 more < .\a:b

有关有效文件名的信息,看这里

要回答您的具体问题: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:

 more < .\a:b

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.

无法言说的痛 2025-01-10 08:12:44

我建议查看正则表达式。它们允许您分解字符串并查看某些特征是否适用。另一种可行的方法是将字符串拆分为 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.

音盲 2025-01-10 08:12:44

您应该查看 正则表达式 并创建一个与任何非法字符,如下所示:

String fileName = "...";
Pattern pattern = Pattern.compile("[:;!?]");
Matcher matcher = pattern.match(fileName);

if (matcher.find())
{
    //Do something when the file name has an illegal character.
}

注意:我尚未测试此代码,但它应该足以让您走上正轨。上面的代码将匹配任何包含 :, ;, `!' 的字符串和 '?'。请随意添加/删除您认为合适的内容。

You should take a look at Regular Expressions and create a pattern which will match any illegal character, something like this:

String fileName = "...";
Pattern pattern = Pattern.compile("[:;!?]");
Matcher matcher = pattern.match(fileName);

if (matcher.find())
{
    //Do something when the file name has an illegal character.
}

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.

擦肩而过的背影 2025-01-10 08:12:44

您可以使用File.renameTo(File dest);

You can use File.renameTo(File dest);.

千纸鹤 2025-01-10 08:12:44

首先获取文件名:

String fileName = fullPath.substring(fullPath.lastIndexOf('\\'), fullPath.length);

创建一个包含文件名中不允许的所有特殊字符的数组。

对于数组中的每个 char,检查 fileName 是否包含它。我猜想,Java 有一个预先构建的 API。

检查这个。

注意:此解决方案假设父级目录存在

Get the file name first:

String fileName = fullPath.substring(fullPath.lastIndexOf('\\'), fullPath.length);

Create an array of all special chars not allowed in file names.

for each char in array, check if fileName contains it. I guess, Java has a pre-built API for it.

Check this.

Note: This solution assumes that parent directory exists

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文