Windows 上的 Java:防止 '/'文件名中的斜杠充当分隔符

发布于 2024-10-15 04:19:18 字数 334 浏览 6 评论 0原文

我必须根据提供给我的字符串创建一个文件。 对于此示例,假设文件名是“My file w/ stuff.txt”。 当 Java 使用以下命令创建文件时,

 File file = new File("My file w/ stuff.txt")

即使默认的 Windows 分隔符是 '\',它也会假定 '/' 斜杠是文件分隔符。因此,将来调用 file.getName() 将返回 " stuff.txt"。这会给我的程序带来问题。

有什么办法可以防止这种行为吗?

I have to create a file based on a string provided to me.
For this example, let's say the file name is "My file w/ stuff.txt".
When Java creates the file using

 File file = new File("My file w/ stuff.txt")

Even though the default windows separator is '\', it assumes that the '/' slash is a file separator. So a future call to file.getName() would return " stuff.txt". This causes problems for my program.

Is there any way to prevent this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

眼泪也成诗 2024-10-22 04:19:18

根据此维基百科页面,Windows API 处理 / 相当于 \。即使您以某种方式设法将 / 嵌入到(例如)File 对象的路径名组件中,Windows 在某些时候也可能会将其视为路径分隔符。

因此您的选择是:

  • 让 Windows 像平常一样处理 /;即让它将该字符视为路径名分隔符。 (用户应该知道这一点。对于 Windows 用户来说,这是一个“计算机素养”的事情。)

  • 如上所述,但向用户发出有关 / 的警告。

  • 检查 /\ 字符,并拒绝这两个字符,表示文件名(即路径名组件)不能包含路径名分隔符。

  • 在尝试创建文件之前,使用某种编码方案对保留字符进行编码。然后,您还必须在需要向用户显示“带斜杠的文件名”的所有点应用(反向)解码方案。

    注意:如果用户可以看到实际的文件路径(例如通过命令外壳),则无法向他们隐藏编码/解码。可以说,这比您最初试图解决的“问题”更糟糕。

    Windows 操作系统不会接受用于此目的的转义方案。您需要使用类似 % 编码 的内容;例如,将 / 替换为 %2F。基本上,您需要通过将斜杠字符替换为操作系统级路径名中的其他字符来“隐藏”Windows 中的斜杠字符!

最佳选择取决于您的申请细节;例如,您是否可以向输入虚假文件名的人报告问题。

According to this Wikipedia page, the Windows APIs treat / as equivalent to \. Even if you somehow managed to embed a / in a pathname component in (for example) a File object, the chances are that Windows at some point will treat it as a path separator.

So your options are:

  • Let Windows treat the / as it would normally; i.e. let it treat the character as a pathname separator. (Users should know this. It is a "computer literacy" thing ... for Windows users.)

  • As above, but with a warning to the user about the /.

  • Check for / AND \ characters, and reject both saying that a filename (i.e. a pathname component) cannot contain pathname separators.

  • Use some encoding scheme to encode reserved characters before attempting to create the files. You must also then apply the (reverse) decoding scheme at all points where you need to show the user their "file name with slashes".

    Note: if the user can see the actual file paths (e.g. via a command shell) you can't hide the encoding / decoding from them. Arguably, that is worse than the "problem" you were trying to solve in the first place.

    There is no escaping scheme that the Windows OS will accept for this purpose. You would need to use something like % encoding; e.g. replace / with %2F. Basically you need to "hide" the slash character from Windows by replacing it with other characters in the OS-level pathname!

The best option depends on details of your application; e.g. whether you can report problems to the person who entered the bogus filename.

游魂 2024-10-22 04:19:18

如果(从外部源)向您提供了一个字符串,那么听起来您无法阻止该字符串包含某些字符。如果您有某种 GUI 来创建字符串,那么您始终可以在那里限制它。否则,无论使用什么方法创建文件都应该检查斜杠,然后返回错误或按照您认为合适的方式进行处理。

If a string is being provided to you (from an external source), it doesn't sound like you can prevent that string from containing certain characters. If you have some sort of GUI to create the string, then you can always restrict it there. Otherwise, whatever method is creating your file should check for a slash and either return an error or handle it as you see fit.

绝影如岚 2024-10-22 04:19:18

由于 Windows 文件名中不允许使用正斜杠或反斜杠,因此应将它们从用于命名文件的字符串中清除。

Since neither forward nor backward slashes are allowed in windows file names, they should be cleaned out of the Strings used to name files.

安稳善良 2024-10-22 04:19:18

那么,你怎么能阻止它成为文件夹分隔符呢?它是一个文件夹分隔符。如果您可以自己决定什么是文件夹分隔符,什么不是文件夹分隔符,那么整个系统就会崩溃。

Well, how could you stop it being a folder separator? It is a folder separator. If you could just decide for yourself what was and what wasn't a folder separator, then the whole system would come crashing down.

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