java.io.File getName()
Description
Method Syntax
public String getName()
Method Argument
Data Type | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns
This method returns a String which denotes the name of the file or directory denoted by this abstract pathname, or the empty string if this pathname’s name sequence is empty.
Compatibility
Requires Java 1.0 and up
Java File getName() Example
Below is a java code demonstrates the use of getName() method of File class. The example presented might be simple however it shows the behaviour of the getFreeSpace() method of File class. We have put a check if the file exists or not before we printed the getName() method.
package com.javatutorialhq.java.examples; import java.io.File; /* * This example source code demonstrates the use of * getName() method of File class. * */ public class FileGetNameExample { public static void main(String[] args) { // initialize File object File file = new File("C:javatutorialhqinputtest_file.txt"); boolean result; // check if file exists result=file.exists(); if(result){ // print message that file exists System.out.println(file.getName() + " exists"); } else{ //print message that the file does not exist System.out.println(file.getName()+" does not exists"); } } }