Introduction to I/O framework
I/O is the interface for computer input/output. Java Core Library Java.io provides a full range of I/O interfaces, including: File system operation, file reading and writing, standard equipment output and so on.
File class and use
① an object of a file class that represents a file or directory on disk.
The ②file class provides a platform-independent way to manipulate files or directories on disk.
The ③file class handles files and file systems directly. For example, delete files, get file length size information.
The ④file class does not provide a way to read from a file or store information in a file.
⑤ Construction Method:
A) File (String directorypath)
b) File (String directorypath,string filename)
c) file (file dirobj,string filename)
⑥file defines a method for getting the standard properties of a file object.
⑦ part of the file class common methods:
A) public String getName ()
b) Public String getParent ()
c) Public File getparentfile ()
d) public String GetAbsolutePath ()
E) public Boolean exists ()
f) Public file[] ListFile ()
g) Public boolean isdirectory ()
h) Public boolean isfile ()
i) public long length ()
j) Boolean Delete ()
K) Boolean mkdir ()
First create a folder named Doc under the D drive and create a txt text file within the folder
1File File =NewFile ("D:\\doc");//Use under \ \ or/,linux under Windows2System.out.println (File.getabsolutepath ());//returns the absolute pathname string for this abstract path name3System.out.println (File.getparent ());//gets the string for the previous level of the directory4System.out.println (File.isdirectory ());//determines whether the file represented by the path name is a directory5System.out.println (File.isfile ());//determines whether the file represented by the pathname is a standard file6System.out.println (File.exists ());//determines whether the file or directory represented by an abstract pathname exists7System.out.println (File.length ());//returns the length of the file represented by this abstract path name8System.out.println (File.delete ());//Delete the file or directory represented by this abstract path name--cannot be deleted if a file exists under the directory
Output results
D:\doc
D:\
True
False
True
0
False
Create directories and directories to create files
1File MyFile =NewFile ("D:\\zhangsan");2System.out.println (Myfile.mkdir ());//Create a directory that does not exist3 4File MyFile2 =NewFile ("D:\\zhangsan\\ceshi.txt");5 Try {6System.out.println (Myfile2.createnewfile ());//Create a file that does not exist under the specified directory7}Catch(IOException e) {8 e.printstacktrace ();9}
Output results
True
True
Enter some characters at random in the Ceshi.txt in the Zhangsan directory and add a picture
Get all the files in the directory
1 string[] files = myfile.list (); // get all the files in the directory 2 for (String f:files) {3 System.out.println (f); 4 }
Output results
Ceshi.txt
Tu.png
Returns all files with the txt suffix
1string[] Files2 = Myfile.list (NewFilenameFilter () {2 3 @Override4 Public BooleanAccept (File dir, String name) {5 returnName.endswith (". txt");//returns all files with the txt suffix6 }7 });8 for(String f:files2) {9 System.out.println (f);Ten}
Output results
Ceshi.txt
Get all the files, names and sizes in the directory
1 File[] files3=myfile.listfiles (); 2 for (File f:files3) {3 System.out.println (F.getname () + "--" +f.length ()); 4 }
Output results
ceshi.txt--140
tu.png--73425
Returns all files in the directory with the TXT suffix name and size
Method One
1File[] Files4=myfile.listfiles (NewFilenameFilter () {2 3 @Override4 Public BooleanAccept (File dir, String name) {5 returnName.endswith (". txt");//returns all files with the txt suffix in the directory6 }7 }); 8 for(File f:files4) {9System.out.println (F.getname () + "--" +f.length ());Ten}
Output results
ceshi.txt--140
Method Two
1File[] Files5=myfile.listfiles (NewFileFilter () {2 3 @Override4 Public BooleanAccept (File pathname) {5 returnPathname.getname (). EndsWith (". txt");6 }7 }); 8 for(File f:files5) {9System.out.println (F.getname () + "--" +f.length ());Ten}
Output results
ceshi.txt--140
I/O stream--file class and usage