10:57 am - Sunday April 30, 2017

String Getname program to get the name of the file or directory mentioned in pathname using getName() method.

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

String Getname program to get the name of the file or directory mentioned in pathname using getName() method.

EXAMPLE PROGRAM




import java.io.File;

public class StrGetname {
	public static void main(String[] args) {
		File f = null;
		File f1 = null;
		String v, v1;
		boolean bool = false;
		try {
			// create new files
			f = new File("C:\\test.txt");
			f1 = new File("C:\\Program Files");

			// get file name or directory name
			v = f.getName();
			v1 = f1.getName();

			// true if the file path exists
			bool = f.exists();

			// if file exists
			if (bool) {
				// prints
				System.out.println("File name: " + v);
			}

			// true if the directory exists
			bool = f1.exists();

			// if the folder exists
			if (bool) {
				// prints
				System.out.print("Folder name: " + v1);
			}
		} catch (Exception e) {
			// if any error occurs
			e.printStackTrace();
		}
	}
}


 

Output

 

File name: test.txt

Folder name: Program

Description

The java.io.File.getName() method returns the last name of the pathname’s name sequence, that means the name of the file or directory denoted by this abstract path name is returned.

Declaration

Following is the declaration for java.io.File.getName() method:

public String getName()

Parameters

 

  • Not Applicable.

 

Return Value

This method returns name of the file or directory or empty string if pathname’s name sequence in empty.

Exception

  • Not Applicable.

BACK

Filed in: Z-A All Java Codes

Comments are closed.