Check for Correct File Name Syntax
What is the most efficient way to check if a String can be used as a file name? Here is my current (bulky) way:
public static boolean isValidFileName(String s) {
try {
File file = new File(s);
file.mkdir();
file.delete();
return true;
}
catch (Exception e) {
return false;
}
}
Thanks!
[363 byte] By [
hodaddya] at [2007-11-26 18:20:08]

# 1
Sorry about the poor code format, above. This is my first posting experience here.
public static boolean isValidFileName(String s) {
try {
File file = new File(s);
file.mkdir();
file.delete();
return true;
}
catch (Exception e) {
return false;
}
}
# 2
1) What happens if the file already exists?2) You should really use file.mkdirs() instead of file.mkdir().3) What is going to cause the exception? mkdir(), mkdirs() and delete() do not throw an exception if they fail; they return false.