• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Reading a file

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Iam trying to read a file from a directory as follows:

String name = new String(filePath + fileName + ".apr");
File file = new File(name);

My problem is that the file extension ".apr" could be in any case(For Example:- "apr","APR","Apr")

So my reading of the file fails if the file extension differs from ".apr" to any of the extension I listed above.

So in short when Iam reading a file,would like to ignore the case of the file extension.Cant rely just on the filename ignoring the file extension as there could be files with the same name but different file extension.

Is there any utility method in java to do it or how could it be done....

Thanks

Pazzuzu
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me if you are using an OS that doesn't distinguish case for the file system, then you wouldn't be having this problem now. Let's assume then that you're running on Linux or Unix or some other real OS. These OSs consider myfile.APR different from myfile.apr, and in fact there could be one of each. Then what would you do?

I would advise checking out the FileFilter interface and implementing a class based on that. You could use the base file name when constructing an instance, then use that instance to get a list of files that match myfile.apr (case insensitive). If you get a list of 0 or 1, you're fine. If you get 2 or more, well then, you've got to have a plan for what to do.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic