How to find out in Java whether a file name in file system is syntactically valid? Method should be independent of the operation system.
Asked
Active
Viewed 132 times
-3
-
4Valid in what sense? Syntactically valid? Refers to an existing file? Can be created? Local filesystems only or also remote? – NPE Nov 16 '14 at 19:23
-
3Whether s a file name is valid depends on the filesystem you are writing it to i.e. it depends on the path and the machine you are on. There is no absolute check for this. – Peter Lawrey Nov 16 '14 at 19:24
-
Syntactically valid. – alex.seluta Nov 16 '14 at 19:30
1 Answers
0
Try this way:
- Check permission to write files
- Use
File.createNewFile()
, the method returnstrue
if file was created -> name is valid

alex
- 8,904
- 6
- 49
- 75
-
Although that's not so helpful if you don't have permission to create new files, it is generally a smart way. Presumably you want to write it eventually after all – Ben Nov 16 '14 at 19:25
-
You might not have access to the directory or it might not exist. Its not clear if this should be invalid or not. If the file fails to be created because it already exists, it is valid. – Peter Lawrey Nov 16 '14 at 19:26
-
@MC Emperror, this mehod should works in different operation systems. – alex.seluta Nov 16 '14 at 19:32
-