-3

How to find out in Java whether a file name in file system is syntactically valid? Method should be independent of the operation system.

Roman C
  • 49,761
  • 33
  • 66
  • 176
alex.seluta
  • 89
  • 2
  • 7
  • 4
    Valid 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
  • 3
    Whether 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 Answers1

0

Try this way:

  1. Check permission to write files
  2. Use File.createNewFile(), the method returns true 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
  • Use try catch or check permission, matter of taste – alex Nov 16 '14 at 19:35