---
title: "How do I check if a file exists in Java?"  
description: "How do I check if a file exists in Java?"  
author: "Anonymous User"  
published: 2015-08-02  
updated: 2015-08-03  
canonical: https://www.mindstick.com/forum/33389/how-do-i-check-if-a-file-exists-in-java  
category: "java"  
tags: ["java", "file"]  
reading_time: 1 minute  

---

# How do I check if a file exists in Java?

How can I [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) a [file exists](https://www.mindstick.com/forum/23208/check-if-a-file-exists-using-python), before opening it for [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)? (equivalent of [Perl](https://www.mindstick.com/interview/2291/what-is-the-characteristic-of-streaming-api-that-makes-it-flexible-run-mapreduce-jobs-in-languages-like-perl-ruby-awk-etc)'s -e $[filename](https://www.mindstick.com/interview/23463/difference-between-include-filename-and-include-filename)).

## Replies

### Reply by Anonymous User

Use java.io :

```
File f = new File(filePathString);if(f.exists() && !f.isDirectory()) { /* do something */ }
```

\


---

Original Source: https://www.mindstick.com/forum/33389/how-do-i-check-if-a-file-exists-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
