checking valid filename

A

Alex

How do I check if a filename is valid in java i.e. if it has the permitted
characters of a filename?
 
H

hiwa

Alex said:
How do I check if a filename is valid in java i.e. if it has the permitted
characters of a filename?
FileFilter or FileNameFilter. For example, you can get filtered file
name list via File#list(FileNameFilter) method.
 
A

Alex

I probably didn't make my question specific enough...

How do I test if a filename is accepted by the local file system?

- For example a file with filename "1aa" will not be accepted in windows
because it starts
with a number.
 
A

Alex

bad example....

a file with filename "aa?" will not be accepted 'cos it has a question mark
 
T

Thomas Kellerer

Alex said:
I probably didn't make my question specific enough...

How do I test if a filename is accepted by the local file system?

- For example a file with filename "1aa" will not be accepted in windows
because it starts
with a number.

Windows does accept a file with the name "1aa" ...

The only way I can see, is to create the file physically on the disk - using
File.createNewFile() - and check any exception.


Thomas
 
A

Alex

Yes that's what I think I'll have to do - even though I don't consider that
'good' programming practice. I was hoping java would have something better
in the API. I'm writing an app which will write to Linux and Windows
partitions at the same time so it needs to check for both OSes.
 
N

nos

Alex said:
Yes that's what I think I'll have to do - even though I don't consider that
'good' programming practice. I was hoping java would have something better
in the API. I'm writing an app which will write to Linux and Windows
partitions at the same time so it needs to check for both OSes.

How about something like this?

try
{
File f = new File("a??");
}
catch .......
 
J

Jon A. Cruz

Alex said:
bad example....

a file with filename "aa?" will not be accepted 'cos it has a question mark

I've made those before. And files with "*", and files with "\", and
files starting with "C:\", all on Linux/Unix systems.
 
J

Jon A. Cruz

Alex said:
Yes that's what I think I'll have to do - even though I don't consider that
'good' programming practice. I was hoping java would have something better
in the API. I'm writing an app which will write to Linux and Windows
partitions at the same time so it needs to check for both OSes.

Even on a single OS, the allowed file names can change per filesystem of
partitions.

FAT vs NTFS is one big difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top