File Class in Java
In this post, you will learn how to work with the File class so that you can use that same knowledge and thus evolve.
- this class provides methods for creating, deleting, getting the absolute path, and checking for a single file at a time.
File (object).createNewFile() |
Create a file in the specified path |
File (object).delete() |
Deletion of a certain file |
File (object).getAbsolutePath() |
Gets the absolute path of the file |
File (object).exists() |
Checks if the data file exists or not |
package com.caffeinealgorithm.programminginjava;
import java.io.File;
import java.io.IOException;
public class FileClass {
private File file = new File("File.txt");
public void Run() {
/*
try {
file.createNewFile();
}
catch (IOException exception) {
System.out.println(exception);
}
*/
file.delete();
// System.out.println(file.getAbsolutePath());
if (file.exists())
System.out.println("The file \"File.txt\" exists.");
else
System.out.println("The file \"File.txt\" does not exist.");
}
}
// The file "File.txt" does not exist.
Don’t forget to watch the video and you can always read this post in Portuguese.