![]() |
In Java programming, working with files is now a more common task. Developing a code to manage file automatization using Java. In this article, we will discuss how to check if a file exists in Java. By using the java.io.File class we will discuss two methods to check if a file exists or not. Approaches to check if a file exists in JavaWe have two ways to get the list of all the files in a directory in Java.
Java Program to Check If a File ExistsBelow are the implementations of the two approaches using the Java programming language. 1. Using exists() Method in java.io.File classThis is the old way to check if a file exists by using the exists() method in the java.io.File class. Now, let us understand this with the help of an example: Java
Output in Console:Explanation of the above Program:
2. Using java.nio.file PackageThis is the new way to check which was introduced in Java New I/O in Java 7. The java.nio.file.Files class provides a method called exists(). Now, let us understand this with the help of an example: Java
Output in Console:Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |