

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determine if two filename paths refer to the same File in Java
The method java.io.File.equals() is used to find if the two file names refer to the same File in Java. This method requires a single parameter i.e.the file object that is to be compared to the other file object. It returns if the file objects are same 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 file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); boolean flag = file1.equals(file2); System.out.print("The two file names refer to the same file? " + flag); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
The two file names refer to the same file? false
Now let us understand the above program.
The method java.io.File.equals() is used to find if the two file names refer to the same File in Java. The boolean value returned by the method is printed. A code snippet that demonstrates this is given as follows −
try { File file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); boolean flag = file1.equals(file2); System.out.print("The two file names refer to the same file? " + flag); } catch(Exception e) { e.printStackTrace(); }
- Related Questions & Answers
- Compare two file paths in Java
- Python Pandas - Determine if two CategoricalIndex objects contain the same elements
- Determine if file or directory exists in Java
- Determine if File or Directory is hidden in Java
- How to determine if all elements are the same in a Java List?
- HTML File Paths
- Make PHP pathinfo() return the correct filename if the filename is UTF-8
- Program to determine if two strings are close in Python
- Checks if two calendar objects represent the same local time in Java
- Write Code to Determine if Two Trees are Identical in C++
- Simple way to find if two different lists contain exactly the same elements in Java
- Python Pandas - Determine if two Index objects are equal
- JavaScript Determine the array having majority element and return TRUE if its in the same array
- Determine if a Preference Node exists in Java
- How to know if two arrays have the same values in JavaScript?
Advertisements