Class File 12.1 manipulate files
io package of documents related to the class itself is the File class.
// constructor class parameter as desired for a given file path public File (String pathname) {} // File class defines several methods and constants public static Final String pathSeparator // constants represent the path delimiter (windows is: " ; ") public static Final String separator // constant path delimiter (windows are:" \ ") Linux is" / " public boolean createNewFile () throws IOException // ordinary file created public boolean the delete () // delete files public boolean EXISTS () //Determining whether a file exists public Boolean the isDirectory () // determines whether a given path is a directory of public Long length () // Returns the size of the file public String [] List () // entire contents of which are listed in the specified directory, only the column the column names public file [] listFiles () // entire contents listed in the specified directory will be listed path public boolean mkdir () // create a directory of public boolean renameTo (file dest) // to rename the existing file
12.1.3 Example: list the entire contents of the specified directory
1. Requirements: Given a directory, listing all the contents of the directory, a given directory there may be sub-folders, requirements can be listed in all of the subfolders of subfolders.
public static void main (String args []) { File F = new new File ( "D:" + the File.separator); // operating path Print (F); } public static void Print (File F) { // called recursively The method of iF (F =! null ) { iF (f.isDirectory ()) { // is a directory File File [] = f.listFiles (); // is listed iF (File =! null ) { for ( int 0 = I; I <file.length; I ++ ) { Print (File [I]); } } } The else { // No direct printing System.out.println (F); } } }
12.2RandomAccessFile class (the operation of the file contents)
public a RandomAccessFile (File File, String MODE) throws a FileNotFoundException // configured to accept the object class File, designated operation path, but in the setting mode needs to be set, r is a read-only; W is a write-only; RW is read; public a RandomAccessFile ( name String, String MODE) throws a FileNotFoundException // configured not use the file class object represents a file, but directly input a fixed file path public void Close () throws IOException // closing operation of public int Read ( byte [] B) throws IOEception // content to read a byte array public Final byte the readByte () throws IOEception // read a byte public Final int the readInt () throws IOEception // read the data from the file integer public void Seek ( Long POS) throws IOEception // set the position of the pointer public Final void writeByte (String S) throws IOEcetion // writes a string to a file, the processing manner byte public Final void writeInt ( int V) throws IOEception // the file data is written to an int, a length of four public int skipByte ( int n- )throws IOEception // pointer to skip a specified byte
If the file to be written does not exist, the system will automatically create