Re: Sanitize file name
Philipp wrote:
...
Is there a way to test if a filename is valid on a certain platform?
This E.G. makes for some interesting results, though I
am not sure if it really helps with the problem. The
programmer would need to specially account for the
'last situation' where the user puts a character in the
name that is used as (or is generally understood to be)
a path separator.
Irritatingly, although Win's path separator is '\', '/'
will apparently also work (here, on this Win XP pro
box).
<sscce>
import java.io.File;
import java.io.IOException;
class TestFileName {
static void testFileName(String name) {
try {
File f = new File(name);
System.out.println( f.getCanonicalPath() );
} catch(IOException ioe) {
System.err.println( ioe.getMessage() + " '" + name + "'");
}
}
public static void main(String[] args) {
testFileName("123.txt");
testFileName("12?3.txt");
testFileName("12[3.txt");
testFileName("12{3.txt");
testFileName("12!3.txt");
testFileName("12/3.txt");
}
}
</sscce>
[OP]
D:\projects\123.txt
Invalid argument '12?3.txt'
D:\projects\12[3.txt
D:\projects\12{3.txt
D:\projects\12!3.txt
D:\projects\12\3.txt
Press any key to continue . . .
[\OP]
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1