---
title: "Discussion on \"Check if file exists at given path using Java and create if NOT\" | Hashnode"
description: "Discussion on \"Check if file exists at given path using Java and create if NOT\". Problem statement : Want to check if a file exists or not at given location and create if not to avoid FileNotFoundexception\nSolution : using Java\npublic void createFileIfDoesNotExists() throws IOException {\nFile usersHTMLFile = new File(System.getPr..."
url: "https://hashnode.com/posts/check-if-file-exists-at-given-path-using-java-and-create-if-not/608e0509a3716542f363fb3b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Check if file exists at given path using Java and create if NOT](https://qa-amitsahu.hashnode.dev/check-if-file-exists-at-given-path-using-java-and-create-if-not?utm_source=hashnode&utm_medium=feed)

Problem statement : Want to check if a file exists or not at given location and create if not to avoid FileNotFoundexception Solution : using Java public void createFileIfDoesNotExists() throws IOException { File usersHTMLFile = new File(System.getPr...

May 2, 2021 · 1 min read · qa-amitsahu.hashnode.dev  
Read the article

-   [#java](https://hashnode.com/tag/java)
-   [#qa](https://hashnode.com/tag/qa)
-   [#testing](https://hashnode.com/tag/testing)
-   [#automation](https://hashnode.com/tag/automation)

## Written by

[Amit Sahu](https://hashnode.com/@qa-amitsahu)

@qa-amitsahu

  
[View profile](https://hashnode.com/@qa-amitsahu)

QA

## Responses 1

Markdown supported · ⌘↵ to post

[Wellington Domiciano](https://hashnode.com/@wldomiciano)

[May 3, 2021](https://hashnode.com/posts/check-if-file-exists-at-given-path-using-java-and-create-if-not/608e0509a3716542f363fb3b/comment/6090276cdc886b4318007f9e)

Nice! I like to use the [`Files`](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/file/Files.html) utility class, look:

```
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String... args) throws IOException {
    final Path path = Paths.get(System.getProperty("user.dir"), "Report.html");

    if (Files.notExists(path))
      Files.createFile(path);
  }
}
```

[Amit Sahu](https://hashnode.com/@qa-amitsahu)

[May 3, 2021](https://hashnode.com/posts/check-if-file-exists-at-given-path-using-java-and-create-if-not/608e0509a3716542f363fb3b/comment/6090276cdc886b4318007f9e#reply-60902933dc886b4318007fe0)

Good approach! this also serves our purpose. [Wellington Domiciano](https://hashnode.com/@wldomiciano)
