[Java iO] _ file class notes
Objectives of this chapter:
Measure the test taker's knowledge about the functions of the file class.
You can use methods in the file class to operate files.
File class
In the entire Io package, the only class that indicates the file is the file class. You can use the File class to create and 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)
Instance operation 1: Create a new file
Public Boolean createnewfile () throws ioexception
Import Java. io. file; import Java. io. ioexception; public class filedemo01 {public static void main (string ARGs []) {file F = new file ("D: \ test.txt "); // instantiate the object try {f. createnewfile (); // create a file based on the specified path} catch (ioexception e) {e. printstacktrace (); // output exception information }}};
Separator problems:
Use backslash: "\" in Windows :"\"
Use the forward slash "/" in Linux :"/"
If you want to maintain the portability of Java programs, it is best to automatically use separators Based on the operating system.
File. Separator;
Import Java. io. file; import Java. io. ioexception; public class filedemo02 {public static void main (string ARGs []) {system. out. println ("pathseparator:" + file. pathseparator); // call the static constant system. out. println ("separator:" + file. separator); // call a static constant }};
The above program is modified as follows:
Import Java. io. file; import Java. io. ioexception; public class filedemo03 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // instantiate the object try {f. createnewfile (); // create a file based on the specified path} catch (ioexception e) {e. printstacktrace (); // output exception information }}};
Instance operation 2: delete a specified file
Public booleadn Delete ()
Import Java. io. file; import Java. io. ioexception; public class filedemo04 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // instantiate the object F. delete (); // delete a file }};
In the file class, you can use the exists () method to determine whether a file exists:
Public Boolean exists ()
After using the exists () method, modify the Code as follows:
Import Java. io. file; import Java. io. ioexception; public class filedemo05 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // instantiate the object if (F. exists () {// Delete F if the file exists. delete (); // delete a file }}};
Instance operation 3: Create and delete files comprehensively
Import Java. io. file; import Java. io. ioexception; public class filedemo06 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "test.txt"); // instantiate the object if (F. exists () {// Delete F if the file exists. delete (); // delete file} else {try {f. createnewfile (); // create a file based on the specified path} catch (ioexception e) {e. printstacktrace (); // output exception information }}}};
Instance operation 4: Create a folder
Public Boolean mkdir ()
Import Java. io. file; import Java. io. ioexception; public class filedemo07 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator + "mldn"); // instantiate the object F. mkdir (); // create a folder }};
Instance operation 5: list all files in the specified directory
Public String [] list ()
Return in the form of a file array: Public file () listfiles ()
Operation 1:
Import Java. io. file; import Java. io. ioexception; public class filedemo08 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator); // instantiate the object string STR [] = f. list (); // list the content in the given directory for (INT I = 0; I <Str. length; I ++) {system. out. println (STR [I]) ;}};
Operation 2:
Import Java. io. file; import Java. io. ioexception; public class filedemo09 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator); // instantiate the object file files of the file class [] = f. listfiles (); // list all content for (INT I = 0; I <files. length; I ++) {system. out. println (files [I]) ;}}};
Instance operation 6: Determine whether a specified path is a directory
Public Boolean isdirectory ()
Import Java. io. file; import Java. io. ioexception; public class filedemo10 {public static void main (string ARGs []) {file F = new file ("D:" + file. separator); // instantiate the object if (F. isdirectory () {// determine whether the directory is system. out. println (F. getpath () + "path is a directory. ");} Else {system. Out. println (F. getpath () +" path is not a directory. ");}}};
Instance operation 7: list all contents of a specified directory
Import Java. io. file; import Java. io. ioexception; public class filedemo11 {public static void main (string ARGs []) {file my = new file ("D:" + file. separator); // operation path print (my);} public static void print (File file) {// recursively call if (file! = NULL) {// determines whether the object is null if (file. isdirectory () {// If the directory is file F [] = file. listfiles (); // list all objects if (F! = NULL) {// determines whether this directory can list for (INT I = 0; I <F. length; I ++) {print (F [I]); // continue to judge because the given path may be a directory.} else {system. out. println (File); // output path }}}};