views:

429

answers:

7

My web application runs on Windows. I would like to run my app on Linux also. I seem to have overcome most of the problems such as path separator, etc.

Real problem now is I get FileNotFoundException when the Java code tries to open a file say Abc.txt when only abc.txt exists. :(

I can't go on changing all the filenames to lowercase or uppercase as i have a whole lot of files. Without changing code much is that any possible way to avoid this?

A: 

It's not clear from your question what is causing the case change of your files. If all your files are lowercase in linux, while they are mixed case on windows, you could just transform the filename to lower case, like so:

new File(filename.toLowerCase())
disown
+2  A: 

Well, first of all I think you should consider moving to a consistent naming scheme, rather than to use some workaround.

Anyway, what about reading in all file names and putting them into a map that contains the lower-case name as a key? You can then look up the correct file name from the map.

This would also allow you to detect a conflict, e.g. two files "FileA.txt" and "FILEA.TXT" in the same directory that have the same lower-case representations, in which case you know that you have to tackle the problem in a completely different way (because you have to know which one you want to have opened, and it's ambiguous, and such a workaround won't do it then).

__roland__
+3  A: 

There is no way to avoid this as the java.io.File API is system-dependent. You have to use the right case when manipulating files on Linux/Unix. Actually, my advice/solution would be to follow strict and portable conventions during development on Windows (e.g. use lower case filenames only or, better, use the exact file name when accessing it programmatically). To be honest, I don't understand why you are trying to load Abc.txt when the filename is abc.txt. This is a bad habit (taught by spending too much time on Windows) rather than a Linux/Unix problem.

Pascal Thivent
let's just say it's not my code but i'm responsible to make the app run on linux.
tony_le_montana
Having a build and test server on all target platforms will discover many of these issues much earlier in the process.
Thorbjørn Ravn Andersen
+1  A: 

Assuming that the files are mixed case on Linux, there is no simple answer to this.

The best I can think of is to have your application list the relevant directories and create an in memory data structure of the actual Linux filenames. Then to do a case-insensitive file open, you split the pathname into components, search the in-memory tree using case-insensitive search, bulid the real (case-sensitive) pathname and use THAT to do the file open.

The problem with this is that it (and indeed your app) cannot cope with the case where you have (say) "foo.txt" AND "Foo.txt" in the same Linux directory.

But the best solution is to change your application so that it works with case-sensitive pathnames.

Stephen C
A: 

There's a solution that has horrible runtime performance but is very simple to implement:

Replace new FileReader(name) with something like

openFile(name);

public FileReader openFile(String name) throws FileNotFoundException {
  File dir = (new File(name)).getParentFile();
  for (File f : dir.listFiles()) {
    if (f.getName().equalsIgnoreCase(name)) {
    return new FileReader(f);
  }
  throw new FileNotFoundException("File not found: " + name);
}

I haven't compiled this code, it may have typos and bugs. I leave them to you to fix.

Carl Smotricz
tempted to do this. :P But not advisable right? will follow best programming practices. :)
tony_le_montana
We're talking on the order of 100ms overhead for opening a file. If it doesn't happen all that often, this solution would do it. If performance is an issue but the directory doesn't change often, you could cache the directory list. But solving the problem at its root would be the best thing. Are you aware of the "rename" tool on Linux?
Carl Smotricz
+4  A: 

Fix it!

Any scheme you devise to circumvent fixing it will be worse in the long run.

Thorbjørn Ravn Andersen
yep. No other go!
tony_le_montana
A: 

Why can't you change a lot of files? If the amount of files is really the only thing that holds you back, then simply write a little script that renames them all to lower-case.

Joachim Sauer