Windows 上的 Java:防止文件名中的“/”斜线充当分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4845866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:06:07  来源:igfitidea点击:

Java on Windows: prevent '/' slash in file name from acting as a separator

javawindowsfileslash

提问by bradvido

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

我必须根据提供给我的字符串创建一个文件。在这个例子中,假设文件名是“我的文件 w/stuff.txt”。当 Java 创建文件时,使用

 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.

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

Is there any way to prevent this behaviour?

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

回答by Stephen C

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

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

So your best options are:

所以你最好的选择是:

  • Let Windows treat the '/' as it would normally; i.e. let it treat the character as a pathname separator.
  • 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.
  • 让 Windows 像往常一样处理“/”;即让它将字符视为路径名分隔符。
  • 如上所述,但会向用户发出有关“/”的警告。
  • 检查 '/' 和 '\' 字符,并拒绝两者都说文件名(即路径名组件)不能包含路径名分隔符。

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

(最好的取决于您的应用程序的详细信息;例如,您是否可以向输入虚假文件名的人报告问题。)

If you try to treat '/' differently from '\', you run the risk of creating more problems than you solve; e.g. if your application needs to be scripted. If you silently strip one or both characters (or turn them into something else) there is a risk that you will create further problems; e.g. unexpected pathname collisions.

如果您尝试将 '/' 与 '\' 区别对待,则可能会产生比解决的问题更多的问题;例如,如果您的应用程序需要编写脚本。如果您默默地剥离一个或两个字符(或将它们变成其他字符),则可能会产生更多问题;例如意外的路径名冲突。

(I originally suggested using the File(URL)constructor on a "file:" URL with a %-escaped '/' character. But even if that worked on the Java side, it won't work on the Windows side.)

(我最初建议File(URL)在带有 % 转义的“/”字符的“文件:”URL 上使用构造函数。但即使这在 Java 端有效,它在 Windows 端也不起作用。)

回答by Jeff Storey

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.

如果(从外部来源)向您提供字符串,听起来您无法阻止该字符串包含某些字符。如果您有某种 GUI 来创建字符串,那么您始终可以将其限制在那里。否则,任何创建文件的方法都应该检查斜线并返回错误或按照您认为合适的方式处理它。

回答by bradvido

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

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

回答by David Heffernan

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.

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