I. Creation, deletion, and renaming of files
File File = new ("/bin/hello.txt");//cannot be created, the system cannot find the path specified
File.createnewfile ();
File File = new file ("Bin/hello.txt");
File.createnewfile ();
File File = new file (".. /hello.txt ");//Create a file in the previous level directory of the current directory
File.createnewfile ();
File File = new file (".. /.. /hello.txt ");//Create a file in the previous level directory of the current directory
File.createnewfile ();
File.exists ()
File Nameto = new file ("Hello.txt");
File.renameto (Nameto);//rename file
Rename file, achieve the effect of moving files, cross directory, require source and destination must be in the same partition
In addition, if the target renaming file already exists, the rename operation will fail
Files are in different partitions and need to use copy of the file instead of renaming
File Nameto = new file ("Src/new Hello.txt");
File.renameto (Nameto);//rename file
File
File.isfile ();
Path (folder)
File.isdirectory ();
File.delete ();//delete files
Ii. creating, deleting, and renaming folders
File Folder1 = new file ("My new Folder");
File Folder2 = new file ("My new Folder/one/two/three/main");
Mkdirdemo (Folder1);
Mkdirsdemo (Folder2);
File NewFolder = new file ("My new Folder-new");
Renamefolder (Folder1, NewFolder);
File Newfolder2 = new file ("My new Folder/one/2/three/main");
Renamefolder (Folder2, newfolder2);//rename failed, renamed folder in constructor, must be the last subdirectory of the multilevel directory
File Newfolder3 = new file ("My new Folder/one/2");
Renamefolder (Folder2, Newfolder3);//rename failed, renamed folder in constructor, must be the last subdirectory of the multilevel directory
Must be in the same partition, and the rename operation will fail if the target's renamed folder already exists
File FOLDERSRC = new file ("My new Folder/one/two/three");
File Folderdest = new file ("My new Folder/one/three");
if (Foldersrc.renameto (folderdest)) {
System.out.println ("Done");
}else {
SYSTEM.OUT.PRINTLN ("fail");
}
File Folder4 = new file ("My new Folder/one/two");//Must be an empty folder to be deleted, if another file or folder is included, the deletion fails
if (Folder4.delete ()) {
System.out.println ("Done");
}else {
System.out.println ("delete fail");
}
}
Create a single directory, the folder if it already exists, it will error, create failure.
private static void Mkdirdemo (File folder) {
if (Folder.mkdir ()) {
System.out.println ("Folder creation Complete");
}else{
if (folder.exists ()) {
System.out.println ("folder already exists without creating.");
}else {
System.out.println ("Folder creation failed");
}
}
/**
* Create a multilevel directory, of course, if the parameter is a first-level directory, it will only create a directory
* The folder will not be error if it already exists, it will not be created if it already exists.
* @param folder2
*/
private static void Mkdirsdemo (File folder2) {
if (Folder2.mkdirs ()) {
System.out.println ("Folder creation complete!");
}
}
/**
* Rename Folder
* @param srcfolder
* @param destfolder
*/
private static void Renamefolder (file srcfolder, file Destfolder) {
if (Srcfolder.renameto (Destfolder)) {
SYSTEM.OUT.PRINTLN ("rename complete");
}else {
System.out.println ("rename failed");
}
}
Third, read the properties of the file
File File = new file ("Text.txt");
Determine if a file exists
System.out.println (File.exists ());
Read the file name, only related to the name in the constructor, and is irrelevant to the existence of the file
System.out.println (File.getname ());
Read file path, relative path
System.out.println (File.getpath ());
Read the file path, absolute path, starting from the root directory of the disk
System.out.println (File.getabsolutepath ());
Read the parent path of the file
System.out.println (File.getparent ());//The result is null, because the path corresponding to file is Text.txt (constructor).
System.out.println (New File (File.getabsolutepath ()). GetParent ());
The size of the read file, in bytes
System.out.println (File.length ()/1000 + "KB");//the unit on the hard drive is 1000 instead of 1024
Determine if a file is a hidden file
Under Linux, files that start with a point are hidden files, and under Windows, right-click to set the hidden properties of the file
System.out.println (File.ishidden ());
Determine if a file is readable
System.out.println (File.canread ());
Determine if the file is writable
System.out.println (File.canwrite ());
Determine if a file is a folder
System.out.println (File.isdirectory ());
Iv. setting the properties of a file
To set a file as writable
File.setwritable (TRUE);
To set a file as not writable
File.setwritable (FALSE);
Set the file to be readable
File.setreadable (TRUE);
Set the file to be unreadable and not writable
File.setreadable (FALSE);
File.setwritable (FALSE);
Set the file to read-only
File.setreadonly ();
Local file operations for Java