File getName() method in Java with Examples Last Updated : 30 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The getName() method is a part of File class. This function returns the Name of the given file object. The function returns a string object which contains the Name of the given file object.If the abstract path does not contain any name then a null string is returned. Function Signature: public String getName() Function Syntax: file.getName() Parameters: This function does not accept any parameters. Return value: This function returns a String value which is the Name of the given File object. Below programs will illustrate the use of the getName() function: Example 1: We are given a file object of a file, we have to get the Name of the file object. Java // Java program to demonstrate the // use of getName() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("c:\\users\\program.txt"); // Get the Name of the given file f String Name = f.getName(); // Display the file Name of the file object System.out.println("File Name : " + Name); } catch (Exception e) { System.err.println(e.getMessage()); } } } Output: File Name : program.txt Example 2: We are given a file object of a directory, we have to get the Name of the file object. Java // Java program to demonstrate the // use of getName() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new File("c:\\users\\program"); // Get the Name of the given file f String Name = f.getName(); // Display the file Name // of the file object System.out.println("File Name : " + Name); } catch (Exception e) { System.err.println(e.getMessage()); } } } Output: File Name :program The programs might not run in an online IDE. please use an offline IDE and set the Name of the file Comment More infoAdvertise with us Next Article File getName() method in Java with Examples andrew1234 Follow Improve Article Tags : Java Java-Functions Java-IO package Java-File Class Practice Tags : Java Similar Reads Field getName() method in Java with Examples The getName() method of java.lang.reflect.Field used to get the name of the field represented by this Field object. When a class contains a field and we want to get the name of that field then we can use this method to return the name of Field. Syntax: public String getName() Parameters: This method 3 min read Class getName() method in Java with Examples The getName() method of java.lang.Class class is used to get the name of this entity. This entity can be a class, an array, an interface, etc. The method returns the name of the entity as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This 1 min read Level getName() method in Java with Examples The getName() method of java.util.logging.Level is used to get the non-localized string name of the Level. This method return name of this method. Syntax: public String getName() Parameters: This method accepts nothing. Return: This method returns non-localized name of this Level. Below programs ill 1 min read Logger getName() Method in Java with Examples getName() method of a Logger class used to get the name of logger. Many times you have to check the logger name so we can use this method to get the logger name. Syntax: public String getName() Parameters: This method accepts nothing. Return value: This method return logger name and it will be null 1 min read Path getFileName() method in Java with Examples The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directo 2 min read File getCanonicalPath() method in Java with Examples The getCanonicalPath() method is a part of the Path class. This function returns the Canonical pathname of the given file object. If the pathname of the file object is Canonical then it simply returns the path of the current file object. The Canonical path is always absolute and unique, the function 3 min read Package getName() method in Java with Examples The getName() method of java.lang.Package class is used to get the name of this package. The method returns the name of the package as a String.Syntax: public String getName() Parameter: This method does not accept any parameter.Return Value: This method returns the name of the package as a String.B 2 min read Path getName(int) method in Java with Examples The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di 2 min read File isFile() method in Java with Examples The isFile() function is a part of File class in Java. This function determines whether the is a file or Directory denoted by the abstract filename is File or not. The function returns true if the abstract file path is File else returns false. Function signature: public boolean isFile() Syntax: file 2 min read Like