Summary of common methods of the Java File class, and summary of the javafile class
The Java File class is very powerful and can basically perform all operations on files using Java. This article will analyze the Java File operation class in detail, and briefly introduce the common methods in the File class. If you need it, you can take a look.
Constructor
Copy codeThe Code is as follows:
Public class FileDemo {
Public static void main (String [] args ){
// Constructor File (String pathname)
File f1 = new File ("c: \ abc \ 1.txt ");
// File (String parent, String child)
File f2 = new File ("c: \ abc", "2.txt ");
// File (File parent, String child)
File f3 = new File ("c:" + File. separator + "abc"); // separator cross-platform separator
File f4 = new File (f3, "3.txt ");
System. out. println (f1); // c: \ abc \ 1.txt
}
}
Creation Method
1. boolean createNewFile () does not exist. true is returned. false is returned.
2. boolean mkdir () to create a directory
3. boolean mkdirs () to create a multi-level directory
Delete Method
1. boolean delete ()
2. boolean deleteOnExit () File deleted after use
Copy codeThe Code is as follows:
Import java. io. File;
Import java. io. IOException;
Public class FileDemo2 {
Public static void main (String [] args ){
File f = new File ("d: \ 1.txt ");
Try {
System. out. println (f. createNewFile (); // if the file exists, false is returned.
System. out. println (f. delete (); // returns false if the object does not exist.
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
Judgment Method
1. boolean canExecute () determines whether the file is executable
2. boolean canRead () determines whether the object is readable.
3. boolean canWrite () determines whether a file can be written.
4. boolean exists () to determine whether the object exists
5. boolean isDirectory ()
6. boolean isFile ()
7. boolean isHidden ()
8. boolean isAbsolute () can be used to determine whether the absolute path file does not exist.
Obtain Method
1. String getName ()
2. String getPath ()
3. String getAbsolutePath ()
4. String getParent () // if no parent directory exists, null is returned.
5. long lastModified () // obtain the last modification time
6. long length ()
7. boolean renameTo (File f)
8. File [] liseRoots () // get the machine drive letter
9. String [] list ()
10. String [] list (FilenameFilter filter)
List files and folders on a disk
Copy codeThe Code is as follows:
Public class FileDemo3 {
Public static void main (String [] args ){
File [] files = File. listRoots ();
For (File file: files ){
System. out. println (file );
If (file. length ()> 0 ){
String [] filenames = file. list ();
For (String filename: filenames ){
System. out. println (filename );
}
}
}
}
}
File Filtering
Copy codeThe Code is as follows:
Import java. io. File;
Import java. io. FilenameFilter;
Public class FileDemo4 {
Public static void main (String [] args ){
File [] files = File. listRoots ();
For (File file: files ){
System. out. println (file );
If (file. length ()> 0 ){
String [] filenames = file. list (new FilenameFilter (){
// File filter directory name file name
Public boolean accept (File file, String filename ){
Return filename. endsWith (". mp3 ");
}
});
For (String filename: filenames ){
System. out. println (filename );
}
}
}
}
}
File [] listFiles ()
File [] listFiles (FilenameFilter filter)
Recursively list all objects
Copy codeThe Code is as follows:
Public class FileDemo5 {
Public static void main (String [] args ){
File f = new File ("e: \ audio ");
ShowDir (f );
}
Public static void showDir (File dir ){
System. out. println (dir );
File [] files = dir. listFiles ();
For (File file: files ){
If (file. isDirectory ())
ShowDir (file );
Else
System. out. println (file );
}
}
}
Move files
Find all. java files under drive D, copy them to the c: \ jad directory, and change the type of all files from. java to. jad.
Copy codeThe Code is as follows:
Public class Test5 {
Public static void main (String [] args ){
File f1 = new File ("d :\\");
MoveFile (f1 );
}
Public static void moveFile (File dir ){
File [] files = dir. listFiles ();
For (File file: files ){
If (file. isDirectory ())
MoveFile (file );
Else {
If (file. getName (). endsWith (". java "))
File. renameTo (new File ("c: \ jad \" +
File. getName (). substring (0, file. getName (). lastIndexOf ('.') + ". jad "));
}
}
}
}
The above are all the attributes and methods of the Java File class. We only need to simply call the above method to complete operations on the specified File. I hope this article will help you.