ArticleDirectory
- Instance operation 1: Create a new file
- Instance operation 2: delete a specified file
- Instance operation 3: Create and delete files comprehensively
- Instance operation 4: Create a folder
- Instance operation 5: list all files in the specified directory
- Instance operation 6: Determine whether a specified path is a directory
- Instance operation 7: list all contents of a specified directory
In the entire Io package, the only class that indicates the file itself is the file class. You can use the File class to create or delete files. To use the File class. First, observe the construction method of the file class. The common construction methods of this class are as follows:
Public file (string pathname)When instantiating the file class, you must set the path
No. |
Method or constant |
Type |
Description |
1 |
Public static final string pathseparator |
Constant |
Path separator (";" for Windows) |
2 |
Public static final string Separator |
Constant |
Path separator ("\" for Windows) |
3 |
Public file (string pathname) |
Structure |
Create a file object and pass in the complete path |
4 |
Public Boolean createnewfile () throws ioexception |
Normal |
Create a new file |
5 |
Public Boolean Delete () |
Normal |
Delete an object |
6 |
Public Boolean exists () |
Normal |
Determine whether a file exists |
7 |
Public Boolean isdirectory () |
Normal |
Determines whether the specified path is in a directory. |
8 |
Public long length () |
Normal |
Size of the returned File |
9 |
Public String [] list () |
Normal |
Lists all the contents of a specified directory, just the name |
10 |
Public file [] listfiles () |
Normal |
Lists all the contents of a specified directory, with a path |
11 |
Public Boolean mkdir () |
Normal |
Create a directory |
12 |
Public Boolean renameto (File DEST) |
Normal |
Rename an existing file |
Instance operation 1: Create a new file
Create a test.txt file on the ddrive
1 Import Java. Io. file;
2 Import Java. Io. ioexception;
3
4 Public Class Test1 {
5 Public Static Void Main (string [] ARGs ){
6 File F = New File ("D:" + file. Separator + "test.txt "); // To increase portability, we recommend that you use file. separator.
7 Try {
8 F. createnewfile ();
9 } Catch (Ioexception e ){
10 E. printstacktrace ();
11 }
12 }
13 }
Instance operation 2: delete a specified file
Delete the created test.txt File
1 Import Java. Io. file;
2
3 Public Class Test2 {
4 Public Static Void Main (string [] ARGs ){
5 File F = New File ("D:" + file. Separator + "test.txt ");
6 If (F. exists ()){ // Check whether the file does not exist. If the file does not exist, you do not need to delete it.
7 F. Delete ();
8 }
9 }
10 }
Instance operation 3: Create and delete files comprehensively
Given a path. If this file exists, it will be deleted. If it does not exist, it will be created.
1 Import Java. Io. file;
2 Import Java. Io. ioexception;
3
4 Public Class Test3 {
5 Public Static Void Main (string [] ARGs ){
6 File F = New File ("D:" + file. Separator + "test.txt ");
7 If (F. exists ()){
8 F. Delete ();
9 } Else {
10 Try {
11 F. createnewfile ();
12 } Catch (Ioexception e ){
13 E. printstacktrace ();
14 }
15 }
16 }
17 }
Instance operation 4: Create a folder
Create a folder using the mkdir () method
1 ImportJava. Io. file;
2
3 Public ClassTest4 {
4Public Static VoidMain (string [] ARGs ){
5File F =NewFile ("D:" + file. Separator + "test ");
6F. mkdir ();//Create a folder
7}
8}
Instance operation 5: list all files in the specified directory
If a directory is provided, the contents in the directory can be listed directly. However, the methods listed include two in the file class:
- Return in the form of a string array: Public String [] list ()
- Return in the form of a file array: Public file [] listfiles ()
Operation 1: Use List () to list all content
1 Import Java. Io. file;
2
3 Public Class Test5 {
4 Public Static Void Main (string [] ARGs ){
5 File F = New File ("D:" + file. separator );
6 String [] STR = f. List ();
7 For (String S: Str ){
8 System. Out. println (s );
9 }
10 }
11 }
All the names are listed above, including the folder name and file name.
Operation 2: Use listfiles () to list
1 Import Java. Io. file;
2
3 Public Class Test6 {
4 Public Static Void Main (string [] ARGs ){
5 File F = New File ("D:" + file. separator );
6 File [] files = f. listfiles ();
7 For (File file: Files ){
8 System. Out. println (File );
9 }
10 }
11 }
The complete path is listed above.
Instance operation 6: Determine whether a specified path is a directory
Determine whether the path is a directory
1 Import Java. Io. file;
2
3 Public Class Test7 {
4 Public Static Void Main (string [] ARGs ){
5 File F = New File ("D:" + file. separator );
6 If (F. isdirectory ()){
7 System. Out. println (F. getpath () + "is a directory ");
8 } Else {
9 System. Out. println (F. getpath () + "not a directory ");
10 }
11 }
12 }
Instance operation 7: list all contents of a specified directory
If a directory is specified, all files in this directory must be listed, including files in subfolders.
1 Import Java. Io. file;
2
3 Public Class Test8 {
4 Public Static Void Main (string [] ARGs ){
5 File F = New File ("D:" + file. separator );
6 Print (f );
7 }
8 Public Static Void Print (file F ){
9 If (F! = Null ){
10 If (F. isdirectory ()){
11 File [] files = f. listfiles ();
12 If (Files! = Null ){
13 For (File file: Files ){
14 Print (File );
15 }
16 }
17 } Else {
18 System. Out. println (f );
19 }
20 }
21 }
22 }
Summary:
- The file class is unique in the Java. Io package and is related to the file itself.
- Common file operations such as file creation and Deletion
- When using the file class to specify the path, be sure to pay attention to the differences between operating systems and try to use separator for segmentation.