# Checking if a file exists - strange runtime error

**URL:** <https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276>\
**Category:** Coding Questions\
**Created:** [April 29, 2020, 4:42pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276 "2020-04-29T16:42:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilHaw](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philhaw/32/8221_2.png) [@PhilHaw](https://discourse.processing.org/u/PhilHaw)\
**Post date:** [April 29, 2020, 4:42pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/1 "2020-04-29T16:42:43Z")

</div>

I’m trying to write a routine that will load from a user’s file (required filename is, say “My patches.txt”) in the data folder but that will default to another file if the user’s file does not exist. I found the ‘file exists test’ below on the forum and it works fine except if the filename differs only due to a difference in upper or lower case.

UsersFile and filename are (global) defined earlier as

```auto
String filename = null;
...
String UsersFile = "My patches.txt";

```

1. If the user’s file does **not** exist then the default file is read in correctly
2. If the user’s file **does** exist and matches “My patches.txt” exactly then the user’s file is read correctly
3. If the user’s file exists but is named, say, “my patches.txt” (just a difference in lower versus upper case) I get the following runtime error

RuntimeException: This file is named my patches.txt not F:\Arduino\BNLibrarian\_5\_03\_BETA\_V5\_Emulations\data\My patches.txt. Rename the file or change your code.

```auto
void loadPresets() {
  BufferedReader reader; 
  String line; 
 
  // See if the user's file exists

  File f = dataFile(UsersFile);
  String filePath = f.getPath();
  boolean exists = f.isFile();
  println(filePath, exists);

  if (exists) {
    filename = dataPath(UsersFile); // Try to load the user's own file first, if that fails load our default
    println(filename);
    reader = createReader(filename);
    println("reader = " + reader);
  } else {
    filename = dataPath("V5 Blue Nebula Patches.txt"); // dataPath("name") returns the full filepath to the file 'data/name'. dataPath() returns the path to the 'data' folder
    println(filename);
    reader = createReader(filename);
  }

```

I can only assume createReader() is case-sensitive to the filename whereas dataFile() is case-insensitive but I can’t figure out how to get around this problem. Any help would be greatly appreciated.

Thanks, Phil.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [April 29, 2020, 4:44pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/2 "2020-04-29T16:44:58Z")

</div>

I’m pretty sure `File` has an `exists()` method. Try that instead of `isFile()`.

---

<div class="post-metadata">

**Author:** ![PhilHaw](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philhaw/32/8221_2.png) [@PhilHaw](https://discourse.processing.org/u/PhilHaw)\
**Post date:** [April 29, 2020, 6:21pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/3 "2020-04-29T18:21:12Z")

</div>

You’re correct, File does have an exists() method but that gives the same runtime error, sadly 🤔

Phil

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [April 29, 2020, 9:32pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/4 "2020-04-29T21:32:46Z")

</div>

`createReader` takes the path of the file based on the `data` folder. Instead of passing it `filename`, pass it `UsersFile`.

```auto
reader = createReader(UsersFile);

```

---

<div class="post-metadata">

**Author:** ![PhilHaw](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philhaw/32/8221_2.png) [@PhilHaw](https://discourse.processing.org/u/PhilHaw)\
**Post date:** [April 29, 2020, 10:14pm UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/5 "2020-04-29T22:14:57Z")

</div>

Thanks again Schred - I didn’t realize that createReader() takes the path to the data folder. I tried your suggestion.

```auto
  File f = dataFile(UsersFile);
  String filePath = f.getPath();
  boolean exists = f.exists();
  println(filePath, exists);

  if (exists) {
    filename = dataPath(UsersFile); // Try to load the user's own patches first, if that fails load ours
    println(filename);
    reader = createReader(UsersFile);
    println("reader = " + reader);
  } else {
    filename = dataPath("V5 Blue Nebula Patches.txt"); // dataPath("name") returns the full filepath to the file 'data/name'. dataPath() returns the path to the 'data' folder
    println(filename);
    reader = createReader("V5 Blue Nebula Patches.txt");
  }

```

Now the runtime error is slightly different. It doesn’t refer to the full path, instead I get:

_RuntimeException: This file is named my patches.txt not My patches.txt. Rename the file or change your code._

Still seems very strange that the runtime thinks the two filenames are different.

Phil.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [April 30, 2020, 8:45am UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/6 "2020-04-30T08:45:48Z")

</div>

Huh. That is very strange indeed. Looks like you’ll need your own `exists()` method that is case sensitive:

```auto
void setup() {
  String fileName = "My patches.txt";

  if (fileExistsCaseSensitive(fileName)) {
    println("'" + fileName + "' exists.");
    createReader(fileName);
  } else {
    println("'" + fileName + "' does not exist");
  }
}

boolean fileExistsCaseSensitive(String fileName) {
  File dataFolder = new File(dataPath(""));

  for (File file : dataFolder.listFiles())
    if (file.getName().equals(fileName))
      return true;

  return false;
}

```

Note: This only works for files that are directly in the `data` directory (not sub-folders), but that shouldn’t be too difficult to implement if you need it. ;D

---

<div class="post-metadata">

**Author:** ![PhilHaw](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philhaw/32/8221_2.png) [@PhilHaw](https://discourse.processing.org/u/PhilHaw)\
**Post date:** [April 30, 2020, 10:55am UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/7 "2020-04-30T10:55:26Z")

</div>

That works beautifully! Thank you so much Schred 🖖

The case-sensitivity must be in the underlying java file handling somewhere. I’m aware of this causing problems moving apps from Windows (case-insensitive) to Linux (case-sensitive).

By the way there’s a small typo in your code that you might want to fix for others finding your solution here: you have createrReader(filename) should of course be createReader(filename) 🙂

Thanks again!

Phil.

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [April 30, 2020, 11:14am UTC](https://discourse.processing.org/t/checking-if-a-file-exists-strange-runtime-error/20276/8 "2020-04-30T11:14:00Z")

</div>

You’re welcome! And thanks for telling me about the typo. ;D
