constructor function
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 delimiter
File f4 =new file (F3, "3.txt");
System.out.println (F1);//c:\abc\1.txt
}
}
Create a method
1.boolean CreateNewFile () does not exist return true existence returns false
2.boolean mkdir () Creating a directory
3.boolean mkdirs () Create a multilevel directory
Delete method
1.boolean Delete ()
2.boolean deleteonexit () file is deleted after use is complete
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 ());//returns False when a file exists
System.out.println (F.delete ());//returns False when the file does not exist
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Judging method
1.boolean CanExecute () Determine if the file is executable
2.boolean canRead () Determine if the file is readable
3.boolean canWrite () Determine if the file is writable
4.boolean exists () determine if the file exists
5.boolean isdirectory ()
6.boolean Isfile ()
7.boolean Ishidden ()
8.boolean Isabsolute () determines whether an absolute path file does not exist or can be judged
Get method
1.String GetName ()
2.String GetPath ()
3.String GetAbsolutePath ()
4.String getParent ()//If no parent directory returns null
5.long lastmodified ()//Gets the last modified time
6.long Length ()
7.boolean Renameto (File f)
8.file[] Liseroots ()//Get Machine drive letter
9.string[] List ()
10.string[] List (filenamefilter filter)
List the files and folders under the 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)
Use recursion to list all files
Copy CodeThe code is as follows:
public class FileDemo5 {
public static void Main (string[] args) {
File f =new file ("e:\\ music");
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);
}
}
}
Moving files
Locate all the. java files under D, copy to the C:\jad directory, and modify 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"));
}
}
}
}
Java File class Usage