Question

I wrote a program which creates a file with the name derived from a users txtarea. But it got me thinking ,what if the user uses whitespaces in his name or any other special symbols, which in turn than becomes a file with this name.

Can stuff go wrong?

Ifso,which signs/symbols should I allow to be used in a file's name?

Thank you for your time

Était-ce utile?

La solution

Ultimately, what symbols are allowed will depend on the local filesystem/OS. However, Java will check for you whether the filename is valid and throw an IOException if it isn't, when you try to create the file via createNewFile() for example. So stuff either won't go wrong or you'll be presented with an exception which you should handle.

See the Wikipedia page on filenames for more details on what's allowed and what's not in different OS/filesystems.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top