Files in Java | Template
Abdulaziz Isakov
Java File Class Methods
- canRead() → Boolean
Definition: The canRead() method is used to check whether we can read the data of the file or not.
2. createNewFile() → Boolean
Definition: The createNewFile() method is used to create a new empty file.
3. canWrite() → Boolean
Definition: The canWrite() method is used to check whether we can write the data into the file or not.
4. exists() → Boolean
Definition: The exists() method is used to check whether the specified file is present or not.
5. delete() → Boolean
Definition: The delete() method is used to delete a file.
6. getName() → String
Definition: The getName() method is used to find the file name.
7. getAbsolutePath() → String
Definition: The getAbsolutePath() method is used to get the absolute pathname of the file.
8. length() → Long
Definition: The length() method is used to get the size of the file in bytes.
9. list() → String[]
Definition: The list() method is used to get an array of the files available in the directory.
10. mkdir() → Boolean
Get Isakovinc’s stories in your inbox
Join Medium for free to get updates from this writer.
Definition: The mkdir() method is used for creating a new directory.
File Operations
We can perform the following operations on a file:
- Create a File
- Get File Information
- Write to a File
- Read from a File
- Delete a File
Creating a File
After creating a new file object and specifying the name of upcoming file. It is time to create the actual file.
Output:
To create a file in a specific directory (requires permission), specify the path of the file and use double backslashes to escape the “\" character (for Windows). On Mac and Linux you can just write the path, like: /Users/name/filename.txt
// When first creating an object from File class you can specify the
// Path in the order shown below.File myObj = new File("C:\\Users\\MyName\\filename.txt");
Writing to a File
In the following example, we will use the class called FileWriter together with its write() method to write some text to the file we created above. Note that when you finish writing to a file you need to close it with close() method.
Output in the console:
The text file:
There is another class called BufferedWriter which is slightly different from File class’ write.
Here compared to myWriter object bufferedWriter shows the result instantaneously.
// BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// bufferedWriter.write("[]");BufferedWriter will not re-write the text, but it will continue writing. If you want to add some text to your file later. You do not have to think about inserting appending=true thing unlike FileWriter does.
If you want to write to a file once or twice use File class’ writer function.
Else if you want to continuously use write() function. Use it from bufferedWriter object.
Reading from a File
In the following example, we use the Scanner class to read the contents of the text file we created previously:
To get more information about the file we can use the methods which were shown at the beginning of the post.
import java.io.File; // Import the File class
public class GetFileInfo {
public static void main(String[] args) {
File myObj = new File("filename.txt");
if (myObj.exists()) {
System.out.println("File name: " + myObj.getName());
System.out.println("Absolute path: " + myObj.getAbsolutePath());
System.out.println("Writeable: " + myObj.canWrite());
System.out.println("Readable " + myObj.canRead());
System.out.println("File size in bytes " + myObj.length());
} else {
System.out.println("The file does not exist.");
}
}
}Delete a File
To delete a file in Java, use the delete() method:
import java.io.File; // Import the File class
public class DeleteFile {
public static void main(String[] args) {
File myObj = new File("filename.txt");
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}For more visit: https://www.w3schools.com/java/java_files.asp
My Telegram channel: Java Stuff








