This week the focus is placed on learning operating Java files.
Learning as follows:
Created in the specified location of a file, directory and create a txt file, and output statements to determine whether to create success.
Package File file type operation; Import java.io.File; Import java.io.IOException; public class Demo1 { public static void main (String [] args) throws IOException {// thrown File File = new new File ( "D : // java directory created " ); boolean b = file.mkdir (); // create a virtual directory IF (b) { System.out.println ( !" directory successfully created " ); File = new new File (" d : // // create the directory java java file created .txt " ); boolean b2 =file.createNewFile (); IF (b2) { System.out.println ( "File successfully created!" ); } the else { System.out.println ( "File creation failed!" ); } } the else { System.out. println ( "directory creation failed!" ); } } }
Delete operations:
You must delete the files inside of something, and then delete the file itself, if the file inside the thing, you can not delete the operation. Delete only an empty file.
Package File file type operation; Import java.io.File; Import java.io.IOException; public class Demo2 { public static void main (String [] args) throws IOException { File File = new new File ( "D: // Create Java // directory file .txt "java created ); IF (File.Exists ()) { // If the file exists boolean b = File.delete (); // delete the file IF (b) { System.out.println ( "delete file successfully" ); } the else { System.out.println ("Failed to delete file" ); } } File = new new File ( "d: // java directory created" ); IF (File.Exists ()) { File.delete (); // delete directory boolean b2 = File. the delete (); IF (b2) { System.out.println ( "successfully remove a directory ..." ); } the else { System.out.println ( "delete directory failed." ); } } } }
Traverse directories:
The output out of the files in the directory:
Package File file type operation; Import java.io.File; public class Demo3 { public static void main (String [] args) { File File = new new File ( "D: \\ c \\ c Language Language Information" ); File Files [] = File.listFiles (); // traverse the directory for ( int I = 0; I <files.length; I ++ ) { System.out.println (Files [I]); } } }
Perform the operation results are as follows:
Reading and writing files:
output stream reader
writer for the input stream
After each new input and output streams must be operated by a respective closing created.
Two kinds of ways to read the file:
1 . // all at once have been read out and output Package File File Class Action 2; Import java.io.File; Import java.io.FileReader; Import a java.io.Reader; public class file read and write operations { public static void main (String [] args) throws Exception { File File = new new File ( "D: //cxy.txt" ); Reader Reader = new new the FileReader (File); char C [] = new new char [1024]; // character array int len = reader.Read (C); reader.Close (); // Close the input stream System.out.println ( "details:" + new new String (C, 0 , len)); } } 2 . // the file reads and outputs data individually Package file file class Action 2; Import java.io.File; Import java.io.FileReader; Import a java.io.Reader; public class file read and write operations {2 public static void main (String [] args) throws Exception { file File = new new File ( "D: //cxy.txt" ); Reader Reader = new newThe FileReader (File); char C [] = new new char [1024]; // array of characters int TEMP = 0 ; int len = 0 ; the while (! (Reader.Read TEMP = ()) = -. 1) { // a reading a C [len ++] = ( char ) TEMP; } reader.Close (); // Close the input stream System.out.println ( "details:" + new new String (C, 0 , len)); } }
Overwriting files:
The original data file covering the entire re-enter data
Package File File Class Action 2; Import java.io.File; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import a java.io.OutputStream; public class Demo3 { public static void main (String [] args) throws {Exception File File = new new File ( "D: // Baidu network disk //cxy.txt" ); OutputStream OUT = new new FileOutputStream (File); String str = "I could not help my life by the day, is a magic fairy to me a small count us "; // cover byte B [] = str.getBytes (); out.write (B); // array to the output stream the out.close (); // Close the output stream } }
File append mode:
Before data does not cover, add new information to the end
Package File File Class Action 2; Import java.io.File; Import java.io.FileOutputStream; Import a java.io.OutputStream; / ** * append file * @author cuixingyu * * / public class Demo4 { public static void main ( String [] args) throws Exception { File File = new new File ( "D: // Baidu network disk //cxy.txt" ); the OutputStream OUT = new new a FileOutputStream (File, to true ); String STR = "my life that I could not help day, magic fairy is for me to say 4785 "; // append mode byte B [] = str.getBytes (); out.write (B); // array to the output stream the out.close (); // Close the output stream } }