Browse DevX
Sign up for e-mail newsletters from DevX

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Performing File Operations With the File Class

You can use the File class (see Tips: "Constructing a File Class" and "Manipulating File Names With the File Class") to perform operations on files that reside on your operating system's file system. These operations include creating directories, obtaining information about the file, renaming the file, deleting the file, and so on. The File class provides some of the following methods:
public boolean exists()         // Answers "Does this file exist?"
public boolean canWrite()       // Answers "Can the program write to this file?"
public boolean canWrite()       // Answers "Can the program read from this file?"
public boolean mkdir()           // Create a directory for this filename
public boolean delete()         // Delete a directory/file with this filename
public boolean renameTo(File file)   // Renames or moves the file/dir
The following code illustrates how to use 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("C:\\foo\\bar\\baz\\myfile.xyz\\myfile.xyz");
4. System.out.println("1::myFile exists = " + myFile.exists());
5. System.out.println("2::can write = " + myFile.canWrite());
6. System.out.println("3::can read = " + myFile.canRead());
8. File myDir = new File(
Ajit Sagar
Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date