constructor function
The 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
The 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 () determines whether the file can be executed
2. Boolean CanRead () determines whether the file is readable
3.boolean CanWrite () determines whether the 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 an absolute path file does not exist or can determine
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
The 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
The 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
The 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.
The 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"));
}
}
}
}
A detailed description of the file class in Java