- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether the given file is an existing file in Java
The method java.io.File.isFile() is used to check whether the given file specified by the abstract path name is an existing file in Java. This method returns true if the file specified by the abstract path name is a file and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("Is file? " + file.isFile()); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
Is file? true
Now let us understand the above program.
The method java.io.File.isFile() checks whether the file is a file or not and the boolean value that is returned by the method is printed. A code snippet that demonstrates this is given as follows −
try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("Is file? " + file.isFile()); } catch(Exception e) { e.printStackTrace(); }
- Related Articles
- Check whether the file is hidden or not in Java
- Check whether a file is a directory in Java
- Check whether the file can be read in Java
- Java Program to Append Text to an Existing File
- Check whether we can write to a file in Java
- Java Program to check whether a file exists or not
- How to add/append content to an existing file using Java?
- How to create a duplicate file of an existing file using Python?
- How to add a JSON string to an existing JSON file in Java?
- fopen() for an existing file in write mode in C
- Golang program to append a string in an existing file
- Check if a file is hidden in Java\n
- Check if the File object refers to an absolute pathname in Java
- Java program to check whether the given number is an Armstrong number
- Check for file or directory in Java

Advertisements