Personal understanding:
The data stream IO is the way to operate, because of the different coding, the operation of the document to produce two. Preferably a byte stream, in order to facilitate watching kanji, (it has been determined that character) character stream may be used. Each genre will be divided into input and output, so you can copy files produced! Note that the Flush () is empty, rather than refreshing ah.
Generally mainly used in IO , that empty the buffer data, that you use when reading and writing flow, in fact, the data is to be read into the memory and data written to a file, when you read the data does not represent your data has been finished, because there are some likely will remain in the memory buffer. At this time if you call the close () method to close the stream read and write, then this part of the data will be lost, so it should be shut down before the first flush stream read and write (), first empty data.
A, IO streams:
. 1, JAVA operations on the data stream by way of, JAVA class for operation in the data stream IO package;
2, IO operation for data transmission between devices;
3, classification:
①, flows:
Input stream (read data)
Output stream (write data)
②, the data type:
Byte stream (can operate any data):
Input stream of bytes
Byte output stream
Jifuryu:
Character-input stream
Character-output stream
note:
If not explicitly stated in what points, divided according to the default data type;
Unless the file with notepad windows open we were able to read only the use of a stream of characters, it is recommended to use a byte stream.
4, IO program writing:
Before use, introduced exception IO package;
In use, an abnormality can be treated IO;
After use, the release of resources.
Second, the byte stream:
1, the output byte stream: outputStream
Subclasses: FileOutputStream file output stream output stream for writing data to the File.
Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo01 { public static void main (String [] args) throws IOException { // create output byte stream object (as is the output , must have a data file must be, can not be a folder) // constructor when specifying file address, if present, the cover! If not, then create! FileOutputStream fos = new new FileOutputStream ( "D: \\ \\ a.txt the Java" ); // write to file a byte write (direct write their number will correspond to the ASCII value) 0- A-97 A-48 65 . fos Write (49 ); fos.write ( 48 ); fos.write ( 48 ); // write to a file in a byte array write (byte [] b) positive walking table, characters are negative, a two byte characters! A multi-byte display? byte [] bytes = {- 66, -67, -68, -69, -70 }; // fos.write (bytes); fos.write (bytes, 2,2 &); // start with the first number the second digit number pass // release resources fos. Close (); } }
Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo02 { public static void main (String [] args) throws IOException { // create output stream of bytes (open writing function ) default flase a FileOutputStream fos = new new a FileOutputStream ( "D: \\ \\ b.txt Java", to true ); // string byte array transfer getByte () fos.write ( "ABC" .getBytes ()); // newline \ r \ the n- fos.write ( "\ r \ the n-wrap the" .getBytes ()); // release resources fos.close(); } }
Exception Handling:
Import java.io.FileNotFoundException; Import java.io.FileOutputStream; Import java.io.IOException; public class Demo03 { public static void main (String [] args) { // handling exceptions: close so on must be performed inside finally, // but the design definition of fos, we first carried out the assignment defines a FileOutputStream fos = null ; the try { fos = new new a FileOutputStream ( "D: \\ \\ b.txt Java", to true ); fos.write ( "ABC" .getBytes ()); } the catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
2, input stream of bytes: InputStream
Subclass: FileInputStream file input stream
Import 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 (from which a file clear reading data) the FileInputStream FIS = new new the FileInputStream ( "D: \\ \\ d.txt Java" ); // read a byte / * . FIS int len = read (); System.out.println (( char) len); // byte int type turn into a char strong len = fis.read (); System.out.println ((char) len); // byte int type turn into strong char Types of len = fis.read (); System.out.println ((char) len); // byte int type turn into a char strong len = fis.read (); System.out.println ((char) len); // byte int type turn into a char strong len = fis.read (); System.out.println ((char) len); // byte int type turn into strong char type len fis.read = (); System.out.println (len); // total of five bytes, when the value is not read, return len -1 * / // byte by byte All data read file int len = 0 ; the while (! (len = fis.read ()) = -. 1 ) { System.out.println (( char ) len); } // release resources fis.close (); } }
Import a java.io.FileInputStream; Import java.io.IOException; public class Demo05 { public static void main (String [] args) throws IOException { // clear the data source the FileInputStream FIS = new new the FileInputStream ( "D: \\ \\ Java d.txt " ); // Create a byte array byte [] bytes = new new byte [2 ]; / * // read a byte array int len = fis.read (bytes); // read the actual effective bytes System.out.println (len); // byte array to a string System.out.println (new new String (bytes)); * / // loop reads a byte array int len = 0 ; the while (! (Len = fis.read (bytes)) = -. 1 ) { the System.out. the println ( new new String (bytes, 0, len)); // starting from the len taken 0 } fis.close (); } }
3, copy the files:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Copy { public static void main(String[] args) throws IOException { //明确数据源 FileInputStream fis=new FileInputStream("D:\\java\\d.txt"); //明确目的地 FileOutputStream fos=new FileOutputStream("D:\\java\\e.txt"); //开始复制 int len=0; while((len=fis.read())!=-1){ fos.write(len); } //释放资源 fis.close(); fos.close(); } }
Third, Jifuryu:
1, the character stream introduction:
Compare byte stream (can operate any data)
①, Chinese operation byte stream data is not particularly convenient, so there have been converted stream;
Stream conversion effect is to convert a byte stream using the character stream;
②, conversion flow is actually a character stream:
= + Character stream code table byte stream
2, code table:
①, ASCII Table: 7 bits of a byte can be represented in the corresponding bytes are positive.
②, GBK: the most commonly used Chinese code table, 20,000 Chinese and symbols. It is two bytes, wherein a portion of the text, at the beginning of the first byte is 1, the beginning of the second byte is 0;
③, unicode: international standard encoding table; whatever text, two bytes are used:
char type is this code table, two bytes.
Based Unicode utf-8, one byte can store data, do not use two bytes of storage, and more standardization of this code table.
3, character-input stream: Reader
Subclasses file input stream FileReader
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 fr = new new the FileReader ( "D: \\ \\ d.txt the Java" ); // create a character array char [] CH = new new char [1024 ]; // a character array an array of characters read int len = 0 ; the while ((len = fr .read (CH)) = -!. 1 ) { System.out.println ( new new String (CH, 0 , len)); } // release resources fr.close (); } }
4, character-output stream: Writer
子类:FileWriter
Import java.io.FileWriter; Import java.io.IOException; public class Demo03 { public static void main (String [] args) throws IOException { // create output character stream FileWriter FW = new new FileWriter ( "D: \\ Java \ \ d.txt " ); // write a character fw.write (100 ); fw.flush (); // write a string fw.write (" Hello " ); fw.flush (); // writing a character array char [] = {CH 'a', '. 1', 'R & lt' }; fw.write (CH); fw.flush (); //Releasing resources fw.close (); } }
5, Flush () and close () the difference:
flush (): The stream buffer flushes the buffer data to the destination, after the refresh, the flow can continue to use. (Write once refresh, mainly used too much data when possible, so as not to bear)
close (): close the resource, but the data will be closed before the buffer is flushed to the first destination, or loss of data, and then close the stream. Flow can not be used. If the write data and more, be sure to refresh While he wrote, can not last refresh, refresh and close to complete by the close.
6, copy the file:
Import java.io.FileReader; Import java.io.FileWriter; Import java.io.IOException; public class the Copy { public static void main (String [] args) throws IOException { // clear the data source the FileReader fr = new new the FileReader ( " D: \\ \\ d.txt the Java " ); // clear destination FileWriter fw = new new FileWriter (" D: \\ \\ d.txt the Java " ); // start copying int len = 0 ; the while (( fr.read = len ()) = -!. 1 ) { fw.write(len); fw.flush(); } fr.close(); fw.close(); } }