Constructors
Copy Code code 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
}
}
Create Method
1.boolean CreateNewFile () does not exist return true exists return false
2.boolean mkdir () Create directory
3.boolean mkdirs () Creating multilevel Catalogs
Delete method
1.boolean Delete ()
2.boolean deleteonexit () file is deleted after use is complete
Copy Code code 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 ());//return False when file exists
System.out.println (F.delete ());//return False when file does not exist
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Judging Method
1.boolean CanExecute () to determine whether the file is executable
2.boolean CanRead () to determine whether the file is readable
3.boolean CanWrite () to determine whether a file is writable
4.boolean exists () to determine whether a file exists
5.boolean isdirectory ()
6.boolean Isfile ()
7.boolean Ishidden ()
8.boolean Isabsolute () determines whether the absolute path file does not exist and 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 ()//To get the last modification 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 files and folders under the disk
Copy Code code 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 Code code 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)
Listing all files recursively using recursion
Copy Code code 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
Find all the. java files under D disk, copy them to the C:\jad directory, and change the type of all files from. java to. jad.
Copy Code code 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"));
}
}
}
}