bradvido
bradvido

Reputation: 2871

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

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?

Upvotes: 4

Views: 8348

Answers (4)

Stephen C
Stephen C

Reputation: 718758

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.

Upvotes: 7

David Heffernan
David Heffernan

Reputation: 612844

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.

Upvotes: 0

bradvido
bradvido

Reputation: 2871

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

Upvotes: 0

Jeff Storey
Jeff Storey

Reputation: 57192

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.

Upvotes: 0

Related Questions