Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve forbidden characters for filenames, in Java?

Tags:

java

filenames

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.

like image 392
Gnoupi Avatar asked Mar 23 '10 09:03

Gnoupi


People also ask

Why are special characters not allowed in filenames?

There is no requirement to delimit the filename in any way (e.g. surround it with quotes or spaces), so encountering such a special char would cause incorrect parsing (i.e is the special char part of the filename or an operator?).

What special characters are allowed in filenames?

Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters. Characters that are NOT supported include, but are not limited to: @ $ % & \ / : * ? " ' < > | ~ ` # ^ + = { } [ ] ; !


1 Answers

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.

like image 141
Kris Avatar answered Sep 22 '22 19:09

Kris