Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Beginner
Feb 2, 1999



Application Security Testing: An Integral Part of DevOps

Manipulating File Names With the File Class

Java defines a File class (see Tip: "Constructing a File Class") that you can use to manipulate the file name and perform file-related operations on the underlying file system (see Tip: "Performing File Operations With the File Class"). Some useful methods for manipulating the file name, which is a text string, include:
public String getAbsolutePath()  // Gets complete path
public String getName()          // Gets the file name (without path)
public String getParent()        // Gets the parent directory
public boolean isDirectory()     // Answers "is this File a directory?"
public boolean isFile()          // Answers "is this File a file?"
The following code excerpt makes use of these methods. Assume that your system has a file "myfile.xyz" under the directory "C:\foo\bar\baz". Also, assume that this code is running in the directory "C:\foo\bar\baz."
1. File myFile = null;
3. myFile = new File("myfile.xyz");
4. System.out.println("1::" + myFile.getAbsolutePath());
6. myFile = new File("foo\\bar\\baz\\myfile.xyz");
7. System.out.println("2::" + myFile.getName());
8. System.out.println("3::" + myFile.getParent());
9. System.out.println("4::" + myFile.isFile());
10. System.out.println("5::" + myFile.isDirectory());
The output of this code is:
2:: myfile.txt
Ajit Sagar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date