Search code examples
processingcase-sensitivecase-insensitive

Processing - loadStrings() case-insensitive


Is there any way to load a text file in Processing while ignoring the case of the file name? I am opening multiple csv files, and some have the extension capitalized, ".CSV" rather than the standard ".csv", which results in errors due to the loadStrings() function being case-sensitive.

String file = sketchPath("test.csv");
String[] array = loadStrings(file);

The above gives the error:

This file is named test.CSV not test.csv. Rename the file or change your code.

I need a way to make the case of the file name or extension not matter. Any thoughts?


Solution

  • Short answer: No. The case-sensitivity of files comes from the operating system itself.

    Longer answer: you could create code that just tries to load from multiple places.

    Another approach would be to use Java's File class, which has functions for listing various files under a directory, then iterating through them and finding the file that you want. More info is available in the Java reference, but it might look something like this:

    String[] array = null;
    File dir = new File(sketchPath(""));
    for(String file : dir.list()){
       if(file.startsWith(yourFileNameHere)){
          array = loadStrings(file);
          break;
       }
    }
    

    I haven't tested this code so you might have to play with it a little bit, but that's the basic idea. Of course, you might just want to rename your files ahead of time to avoid this problem.