# Check if file exists or not

**URL:** <https://discourse.processing.org/t/check-if-file-exists-or-not/39317>\
**Category:** Beginners\
**Created:** [October 17, 2022, 12:17pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317 "2022-10-17T12:17:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [October 17, 2022, 12:17pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/1 "2022-10-17T12:17:03Z")

</div>

As the title says, I want to check whether a file exists. I am not trying to look at the data folder because I think you just have to use dataFile().  
At the moment I just have to see if a specific file is in the sketch folder.  
I have written the following code.

```auto
String[] lines1 = loadStrings("positions1.txt"); // file exists or not but you get a 
  if (lines1==null){
    println("nothing is here");
  }

```

However this returns some sort of error message about the file might not exist (obviously) and if I have to look at over a hundred files it can become quite a lot of error messages.  
Is there any simple way to check whether a file exists without getting this kind of error message?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 17, 2022, 1:40pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/2 "2022-10-17T13:40:09Z")

</div>

Hi

> [@How to check if a file is exist in data folder by code?](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589):
>
> I have to check if a file is exist in data folder while the code is running. How to achieve that function?

---

<div class="post-metadata">

**Author:** ![Pieter](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Pieter](https://discourse.processing.org/u/Pieter)\
**Post date:** [October 17, 2022, 3:09pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/3 "2022-10-17T15:09:26Z")

</div>

I haven’t checked if this applies in this case, but you may use a try/catch block to discard any thrown exceptions.

> **[Reference](https://processing.org/reference/catch.html)**
>
> The catch keyword is used with try to handle exceptions. Sun's Java documentation defines an exception as "an event, which occurs during the execution of a program, that disrupts the nor…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 17, 2022, 4:34pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/4 "2022-10-17T16:34:08Z")

</div>

> [@jensemand](#):
>
> Is there any simple way to check whether a file exists without getting this kind of error message?

Check `if (!dataFile(name).isFile()) table = createAndSaveTableFile(name);`  
at file “Card\_Functions.pde” within function **loadCards():**

> [@The operator \> is undefined for the argument - HOW TO RESOLVE ERROR (OOP)](https://discourse.processing.org/t/the-operator-is-undefined-for-the-argument-how-to-resolve-error-oop/38705/6#card_functionspde-3):
>
> If you wish you can take a look at a very old deck of cards implementation I did a long time ago: [Picking cards at random, then excluding those from further picking. - Processing 2.x and 3.x Forum](https://forum.Processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-#Item_2) It’s split into 2 sketches: 1 creates & saves the “.csv” file, while the other loads it and makes a deck of cards out of it. For convenience I’ve merged them now into 1 sketch w/ 4 “.pde” tabs: “Table\_CSV\_Card\_Pack.pde”: /\*\* \* Table CSV Card Pack (v1.0.1) \* by GoToLoop (2022/Sep/13) \* \* htt…

---

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [October 17, 2022, 10:00pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/5 "2022-10-17T22:00:44Z")

</div>

Sorry but this didn’t help because when I write the following:

```auto
BufferedReader reader;
String line;
boolean stopReading=false;
 
void setup() {
  // Open the file from the createWriter() example
  reader = createReader("positions1.txt");
  while(!stopReading){
    try {
      line = reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
      line = null;
    }
    if (line == null) {
    // Stop reading because of an error or file is empty
      println("there is no more text");
      stopReading=true;
      noLoop();  
    } else {
      println(line);
    }
  }
}
 
void draw() {
  
}

```

it gives an nullpointerexception. It does this because I have changed the filename. It would be great if I can just change the filename and either it says there is no text if the file doesn’t exists and print the lines if the file exists

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 17, 2022, 10:11pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/6 "2022-10-17T22:11:03Z")

</div>

Add `if (!dataFile("positions1.txt").isFile()) exit();`  
before `reader = createReader("positions1.txt");`

---

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [October 18, 2022, 10:08pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/7 "2022-10-18T22:08:29Z")

</div>

This just gives me a NullPointerException sadly.

```auto
BufferedReader reader;
String line;
boolean stopReading=false;
 
void setup() {
  // Open the file from the createWriter() example
  if (!dataFile("positions1.txt").isFile()) exit();
  reader = createReader("positions1.txt");
  while(!stopReading){
    try {
      line = reader.readLine();
    } catch (IOException e) {
      e.printStackTrace();
      line = null;
    }
    if (line == null) {
    // Stop reading because of an error or file is empty
      println("there is no more text");
      stopReading=true;
      noLoop();  
    } else {
      println(line);
    }
  }
}
 
void draw() {
  
}

```

The problem seems to be in the catch()-part of the code. I have looked around but I can’t find anything about catching a NullPointerException.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 6, 2025, 2:42pm UTC](https://discourse.processing.org/t/check-if-file-exists-or-not/39317/8 "2025-10-06T14:42:59Z")

</div>

> [@jensemand](#):
>
> The problem seems to be in the catch()-part of the code. I have looked around but I can’t find anything about catching a NullPointerException.

Replace:  
`IOException e`

with:  
`Exception e`

I tested with code from here:

_[try / Reference / Processing.org](https://processing.org/reference/try.html)_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/3/833f624e8195c652b8d77fa2beb6d8d519702ed7.png)

An old post but was related to some work I was doing and updating for others.

`:)`
