- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get the name of the file and path in Java
The name of the file and the name of the path can be obtained using the methods java.io.File.getName() and java.io.File.getPath() respectively. The getName() returns the name of the file or the directory. The getPath() returns the abstract pathname in the form of a pathname string.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("File name: " + file.getName()); System.out.println("Path name: " + file.getPath()); } }
The output of the above program is as follows −
Output
File name: demo1.java Path name: C:/jdk11.0.2/demo1.java
Now let us understand the above program.
The file name and path name of the file is printed using the methods getName() and getPath() respectively. A code snippet that demonstrates this is given as follows −
File demo1 = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("File name: " + demo1.getName()); System.out.println("Path name: " + demo1.getPath());
- Related Articles
- How to get the file name from the file path in Python?
- Get Absolute path of a file in Java
- Golang program to get the name of the file from the absolute path
- C# Program to get the name of the file from the absolute path
- Get the path of the file selected in the JFileChooser component with Java
- Get the absolute path for the directory or file in Java
- How to get left substring in MySQL from a column with file path? Display the entire file path string excluding the file name?
- How to get file name from a path in PHP?
- Get the Full Path of a File in Linux
- Get file extension name in Java
- Java Program to get the File object with the absolute path for the directory or file
- How to Get File Owner’s Name in Java?
- Java Program to get the name of the parent directory of the file or directory
- Java Program to get name of specified file or directory
- How to get the absolute path of a file using tkFileDialog(Tkinter)?

Advertisements