tags:

views:

343

answers:

4

There are some restricted characters (and even full filenames, in Windows), for file and directory names. This other question covers them already.

Is there a way, in Java, to retrieve this list of forbidden characters, which would be depending on the system (a bit like retrieving the line breaker chars)? Or can I only put the list myself, checking for the system?


Edit: More background on my particular situation, aside from the general question.

I use a default name, coming from some data (no real control over their content), and this name is given to a JFileChooser, as a default file name to use (with setSelectedFile()). However, this one truncates anything prior to the last invalid character.

These default names occasionally end with dates in a "mm/dd/yy" format, which leaves only the "yy", in the default name, because "/" are forbidden. As such, checking for Exceptions is not really an option there, because the file itself is not even created yet.

Edit bis: Hmm, that makes me think, if JFileChooser is truncating the name, it probably has access to a list of such characters, can be interesting to check that further.

Edit ter: Ok, checking sources from JFileChooser shows something completely simple. For the text field, it uses file.getName(). It doesn't actually check for invalid characters, it's simply that it takes the "/" as a path separator, and keeps only the end, the "actual filename". Other forbidden characters actually go through.

A: 

Have a look at this link for some info on how to get the OS the application is running on. Basically you need to use System.getProperty("os.name") and do an equals() or contains() to find out the operating system.

Something to be weary of though is that knowing the OS does not necessarily tell you the underlying file system being used, for example a Mac can read and write onto the FAT32 file system.

source: http://www.mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/

grahamrb
+3  A: 

When it comes to dealing with "forbidden" characters I'd rather be overcautious and ban/replace all "special" characters that may cause a problem on any filesystem.

Even if technically allowed, sometimes those characters can cause weirdness.

For example, we had an issue where the PDF files were being written (successfully) to a SAN, but when served up via a web server from that location some of the characters would cause issues when we were embedding the PDF in an HTML page that was being rendered in Firefox. It was fine if the PDF was accessed directly and it was fine in other browser. Some weird error with how Firefox and Adobe Reader interact.

Summary: "Special" characters in file names -> weird errors waiting to happen

Ultimately, the only way to be sure is to use a white-list.

Kris
Define "special character".
Michael Borgwardt
+1 for the whitelist
Péter Török
+1 On most UNIX-y file systems the only forbidden characters are `/` and the nul-character `\0`.
Joachim Sauer
+2  A: 

Having certain "forbidden characters" is just one of many things that can go wrong when creating a file (others are access rights and file and path name lengths).

It doesn't really make sense to try and catch some of these early when there are others you can't catch until you actually try to create the file. Just handle the exceptions properly.

Michael Borgwardt
The fact is, this name is a default one retrieved from some data, so it may contain characters which are not suitable for a filename. This default one is given to a JFileChooser, and this one is truncating everything prior to a forbidden character. So in such case, I would need to replace these characters before, so that the default one makes sense. In this context, there is no exception thrown, as the file was not created yet.
Gnoupi
Can't you make a safe default? (like using only ascii-7?). On the other hand you can: 1) pass your candidate default to the JFileChooser 2) ask for the truncated string (without displaying the dialog) 3) if the returned filename is shorter, you know where the forbidden char is.
helios
My part with the JFileChooser was in fact a problem only when using path separators, as described now in my question. For other forbidden characters, the JfileChooser doesn't actually filter anything, it just returns what was entered as file name. Your solution is then probably the easiest, checking simply when trying to actually write the file, and handling the exception.
Gnoupi
+1  A: 

Have you tried using File.getCanonicalPath and comparing it to the original file name (or whatever is retrieved from getAbsolutePath)?

This will not give you the actual characters, but it may help you in determining whether this is a valid filename in the OS you're running on.

Lior