File.exists() -- case-insensitive

N

N.V.Dev

Hi,

Wondering if there is a way to check if the file exists having case insentive.

For example, I have a file in Unix Alex.doc

If I try checking for the file

File file = new File ("alex.doc");
File.exists () should return true


If this is not possible, is there any easy work-around

I understand, I could do an "alex.doc".toUpper() or "alex.doc".toLower()

and check the file. What if the file is in Mixed case.


Let me know how to resolve this.

Thanks in advance,
Dev
 
D

David Hilsee

N.V.Dev said:
Hi,

Wondering if there is a way to check if the file exists having case insentive.

For example, I have a file in Unix Alex.doc

If I try checking for the file

File file = new File ("alex.doc");
File.exists () should return true


If this is not possible, is there any easy work-around

I understand, I could do an "alex.doc".toUpper() or "alex.doc".toLower()

and check the file. What if the file is in Mixed case.

A simple solution could involve getting a directory listing (File.list())
and searching for the file name using java.lang.String.equalsIgnoreCase().
 
M

Michael Borgwardt

No, it shouldn't, because in Unix, that's a completely different file.
A simple solution could involve getting a directory listing (File.list())
and searching for the file name using java.lang.String.equalsIgnoreCase().

The only solution, I'd say.
 
A

Anony!

David Hilsee said:
A simple solution could involve getting a directory listing (File.list())
and searching for the file name using java.lang.String.equalsIgnoreCase().

Thats means "loading" the directory in a tree like structure I assume.
Sounds like alot of work for something simple.

AAA
 
A

Andrew Thompson

Thats means "loading" the directory in a tree like structure I assume.

N.V.'s example suggests to me that thay are
only interested in the current directory.

That being the case, David's File.list()
would return an array that you could loop
through.

Even if you want to check sub-directories as
well, it is not that much more complicated,
and can be done without the need for a 'tree
like structure'. Though the 'tree structure'
may make sense in some situations, I do not
feel it is necessary or justifiable here.
 
S

Sean W Thomson

Hi,

Wondering if there is a way to check if the file exists having case insentive.

For example, I have a file in Unix Alex.doc

If I try checking for the file

File file = new File ("alex.doc");
File.exists () should return true


If this is not possible, is there any easy work-around

I understand, I could do an "alex.doc".toUpper() or "alex.doc".toLower()

and check the file. What if the file is in Mixed case.


Let me know how to resolve this.

Thanks in advance,
Dev

Can you not just retrive the canonical path to the file and check it?
It will return the case the file (well the file its looking at) is in
regardless of what you use to create the file instance.

SeanWT
 
A

Anony!

Sean W Thomson said:
(e-mail address removed) (N.V.Dev) wrote in message

Can you not just retrive the canonical path to the file and check it?
It will return the case the file (well the file its looking at) is in
regardless of what you use to create the file instance.

SeanWT

what u mean by canonical path?

AaA
 
S

Sean W Thomson

For example you could make two methods like this

public String getExactFileName(File f) {
String returnVal;
try {
returnVal = f.getCanonicalPath();
returnVal =
returnVal.substring(returnVal.lastIndexOf(File.separator)+1);
}
catch(IOException e) {
returnVal = "";
}
return returnVal;
}
public boolean fileExistsCaseSensative(File f) {
return getExactFileName(f).equals(f.toString());
}

You could use them like
String goodName = "TeStInG.example", badName =
"tEsTiNg.example";
File goodFile = new File(goodName);
if (!goodFile.exists())
try {
goodFile.createNewFile();
goodFile.deleteOnExit();
}
catch(IOException e) {
System.err.println("Error creating example file");
System.exit(1);
}
File badFile = new File(badName);
System.out.println(fileExistsCaseSensative(goodFile) + " " +
fileExistsCaseSensative(badFile));

Which outputs "true false", it checks that the filename the file
instance is constructed with is the same as the file its actually
looking at (case sensative)

I hope this helps

(Sorry about the sloppy code im only 16 and still learning java)

SeanWT
 
S

Sean W Thomson

Opps, toString() returns the same as getName() but I meant to use the
latter anyway. Sorry
 
D

Does It Matter

Hi,

Wondering if there is a way to check if the file exists having case insentive.

For example, I have a file in Unix Alex.doc

If I try checking for the file

File file = new File ("alex.doc");
File.exists () should return true


If this is not possible, is there any easy work-around

I understand, I could do an "alex.doc".toUpper() or "alex.doc".toLower()

and check the file. What if the file is in Mixed case.


Let me know how to resolve this.

The quick solution is create a method that gets the lists of files for
you and does a case insensitive comparison.

Question: how do you handle the situation when the file "Alex.doc" and
"alex.doc" exist in the same directory? This is possible in UNIX. If the
documents are user created then you have to consider this. If the
documents are created by you then just make it a convention to write them
all one case (upper or lower).
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top