- 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
Java Program to get name of specified file or directory
The name of the specified file or directory can be obtained using the method java.io.File.getName(). The getName() returns the name of the file or the directory and it requires no parameters.
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 + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File name: " + file.getName()); } }
The output of the above program is as follows −
Output
File name: demo1.txt
Now let us understand the above program.
The name of the specified file is obtained using the method java.io.File.getName() and then printed. A code snippet that demonstrates this is given as follows −
File file = new File("C:" + File.separator + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File name: " + file.getName());
- Related Articles
- Java Program to get the name of the parent directory of the file or directory
- Java Program to delete file or directory
- Java Program to get name of parent directory
- Java Program to rename a file or directory
- Java Program to get the File object with the absolute path for the directory or file
- Golang Program to get the list the name of files in a specified directory
- Create temporary file in specified directory in Java
- Java Program to delete a file or directory when the program ends
- Java Program to check if a file or directory is readable
- Get the absolute path for the directory or file in Java
- Java Program to get Size of Directory
- Rename file or directory in Java
- C# Program to get the name of root directory
- Delete the file or directory in Java
- Check for file or directory in Java

Advertisements