Details and simple examples of Java File class
- 2020-09-28 08:53:51
- OfStack
Details and simple examples of Java File class
File() : constructor, which typically creates file objects based on the specified location of the file.
CanWrite() : Returns whether the file is writable.
CanRead() : Returns whether the file is readable.
CompareTo(File pathname) : Checks the order between the specified file paths.
Delet() : Removes the file from the file system.
DeleteOnExit() : Removes a file from the system when the program completes successfully.
Equals(Object obj) : Checks whether the pathname of a particular object is equal.
Exists() : Determine if the folder exists.
GetAbsoluteFile() : Returns the full path to the file.
GetAbsolutePath() : Returns the full path to the file.
GetName() : Returns the file name.
GetParent() : Returns the file parent directory path.
GetPath() : Returns the potential relative path of the file.
GetParentFile() : Returns the path to the folder where the file is located.
HashCode() : Returns the file hash code.
IsDirectory() : Determines whether the path indicates a file.
IsFile() : Determines whether the path indicates a file.
LastModified() : Returns the last modified time flag for the file.
Length() : Returns the file length.
List() : Returns a list of files and directories.
Mkdir() : Generates the specified directory.
RenameTo(File dest) : Rename file.
SetReadOnly() : Sets the file to be readable.
ToString() : Returns a string of file status.
ToURL() : Converts the path string of a file to URL
File.GetCreationTime Read creation time
File.SetCreationTime Set the creation time
2. File.GetLastAccessTime Read the last access time
3. File.SetLastAccessTime Set the last access time
File.GetLastWriteTime Read the last modification time
4. File.SetLastWriteTime Set the last modification time
File.GetAttributes Read file properties
File.SetAttributes Set file properties
Practice chapters:
import java.io.File;
public class FileContent {
/* File Common USES of classes:
1 , the method of establishing the file
*/
public static void createFile(){
/* Window Method to create a file in */
File file1 = new File("C:\\tmep\\myword.txt");
/* A more secure method of creating documentation */
File file2 = new File("c:"+File.separator+"temp"+File.separator+"myword.java");
//File.separator Is the symbol for the file path ==\\
}
/* Introduction to related properties */
public static void main(String[] args){
File myFile = new File("C:"+File.separator+"word.txt");
try{
// Create a file
//myFile.createNewFile();
}catch(Exception ex){
}
// Gets the name of the file ==word.txt
System.out.println(myFile.getName());
// Gets the path to the file ==c:\word.txt
System.out.println(myFile.getPath());
// Determine if the document is complete
System.out.println(myFile.isAbsolute());
// Gets the root directory of the file ==c:\
System.out.println(myFile.getParent());
myFile.exists(); // Determine if the file exists
System.out.println(" Determine if it is a directory :"+myFile.isDirectory());
System.out.println(" Determine if it's a file :"+myFile.isFile());
System.out.println(" Determine if it is a hidden file :"+myFile.isHidden());
System.out.println(" Determine if it is readable :"+myFile.canRead());
System.out.println(" Determine if it is writable :"+myFile.canWrite());
//mkdir(); Create a single-level directory
//mkdirs(); Create multilevel directories
//createNewFile(); Create a file
// try{
// File tmp = File.createTempFile("foo", "tmp");// Create temporary files
// System.out.println(" The temporary file just created is at: " + tmp.getCanonicalPath());
// }catch(Exception ex){
// }
show();
}
// Get all the tails And calculate the rest of the space
public static void show() {
File[] roots = File.listRoots();// Take all the roots, if yes windows The system will then get all the disks
for (int i = 0; i < roots.length; i++) {
System.out.println(roots[i]);
System.out.println("Free space = " + roots[i].getFreeSpace());
System.out.println("Usable space = " + roots[i].getUsableSpace());
System.out.println("Total space = " + roots[i].getTotalSpace());
System.out.println();
}
}
}
If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!