File:.. 1 2. Files directory (folder) 3 pathname
IO stream for processing data transfer between devices
Press operation data is divided into two streams: byte stream and character stream.
com.oracle.demo01 Package; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo01 { public static void main (String [] args) throws IOException { // create a byte output stream object // when this construction method designated addresses, Overwrite if exists, is automatically created if does not exist! (Must specify the file folder can not be) a FileOutputStream fos = new new a FileOutputStream ( "D: \\ \\ d.txt Java"); // write a byte write to the file (int b) ASCII code // 0 -48 // a -49 // -65 a / * fos.write (49); fos.write (48); fos.write (48); * / // byte array to write a file write ( byte [] B) // the BCDE byte [] bytes = {- 66, -67, -68, -69, -70}; /*fos.write(bytes);*/ // where to start transmission of several fos.write (bytes, 2,2 &); // release resources fos.close (); } }
com.oracle.demo01 Package; Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo02 { public static void main (String [] args) throws IOException { // create a byte output stream (open writing function) a FileOutputStream fos = new new a FileOutputStream ( "D: \\ \\ d.txt Java", to true); // string ---> byte array the getBytes (); fos.write ( " ABC ".getBytes ()); // newline \ R & lt \ n- fos.write (" \ R & lt \ n-change line ".getBytes ()); // release resources fos.close (); } }
If you do not open the writing mode, do not add any properties, the default is false
Recursion: In the current method which calls itself recursively called
Method void public () { System.out.println ( "recursive call itself"); // current method calls itself in Method (); }
Recursion can be divided in two forms, a direct recursion, the other is indirect recursion
The method itself is a recursive direct call itself, can be called indirect recursion is A B, B calls in C, C A call to this form
Byte stream: What files can be copied
Byte output stream :: OutputStream
If after the end of Stream will certainly see as a stream of bytes, if not the end Stream is a stream of characters
close (): Before the release of resources to shut down automatically refresh
flush (): write once refreshed!
Clear data source: FileOutputStream fis = new FileOutputStream ( "write path");
com.oracle.demo01 Package; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.IOException; public class Demo04 { public static void main (String [] args) throws IOException { // create a byte input stream (which clearly read from the file data) the FileInputStream new new FIS = the FileInputStream ( "D: \\ \\ d.txt Java"); // read a byte / * int len = fis.read () ; System.out.println ((char) len); len = fis.read (); System.out.println ((char) len); len = fis.read (); System.out.println ((char) len ); len = fis.read (); System.out.println ((char) len); len = fis.read (); System.out.println ((char) len); fis.read = len (); System.out.println (len); * / // read byte by byte file all data int len = 0; the while ((len = fis.read ()) = -. 1!) { System.out.println (( char) len); } // release resources fis.close (); } }
Input stream of bytes: InputStream
FileInputStream file data to read the input stream of bytes
1. 2. Create a data file to find the target input channel 3. The data resource file 4. Close
com.oracle.demo01 Package; Import a java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.IOException; public class Demo05 { public static void main (String [] args) throws IOException { // clear the data source = new new FIS the FileInputStream the FileInputStream ( "D: \\ \\ d.txt Java"); // Create a byte array byte [] bytes = new new byte [2]; / * read a byte array // int len = fis.read (bytes); valid number of bytes actually read // System.out.println (len); // byte array ----> byte string System.out.println (new string (bytes )); * / // read a byte array int len = 0; ! the while ((len = fis.read (bytes)) = -. 1) { System.out.println (new new String (bytes, 0, len)); } // release resources fis.close (); } }
Character stream: you can only copy the text file, what is a text file, open Notepad can read to understand is a text file!
The character input stream the base class: Reader
Transfer byte character streams: InputStreamReader // read character input byte stream
com.oracle.demo02 Package; Import java.io.FileNotFoundException; Import java.io.FileReader; Import java.io.IOException; public class Demo01 { public static void main (String [] args) throws IOException { // Create a character input stream the FileReader the FileReader new new fr = ( "D: \\ \\ d.txt Java"); int len = 0; // read character by character while ((len = fr.read () ) = - 1!) { System.out.println ((char) len); } // release resources fr.close (); } }
com.oracle.demo02 Package; Import java.io.FileNotFoundException; Import java.io.FileReader; Import java.io.IOException; public class Demo02 { public static void main (String [] args) throws IOException { // Create a character input stream the FileReader the FileReader new new fr = ( "D: \\ \\ d.txt Java"); // Create a character array char [] = new new CH char [1024]; // read a character array of a character array int len = 0; the while ((len = fr.read (CH)) = -. 1!) { System.out.println (new new String (CH, 0, len)); } // release resources fr.close (); } }
Character-output stream: writer
Writer is the top-level parent of all the characters of the output stream, and this class is an abstract class. Subclass generally used, most commonly subclass FileWriter.
{class Demo01Writer public public static void main (String [] args) throws IOException { // Create. 1 FileWriter FileWriter new new FW = ( "fiel05.txt"); // write 2 fw.write ( "starting in all but exceptional" ); // 3 refresh fw.flush (); // release. 4 fw.close (); } }
java.io.FileWriter Import; Import java.io.IOException; public class Demo03 { public static void main (String [] args) throws IOException { // create output character stream FileWriter fw = new FileWriter ( "D : \\ java \ \ d.txt "); // write a character fw.write (100); fw.flush (); // write a character fw.write (" starting in all but extraordinary "); // write characters array char [] = {CH 'A', '. 1', 'R & lt'}; fw.write (CH); fw.flush (); // release resources fw.close (); } }