- IO stream (input stream, output stream)
- Byte stream, character stream
1. byte stream:
-
- InputStream, OutputStream
- InputStream abstracts the way the application reads data;
- OutputStream abstracts the way the application writes out data;
2.eof=end read-1 reading to the end
3. Basic method of input stream:
-
- int B=in.read (); reads a byte unsigned fill to int low eight bits;-1 is EOF;
- In.read (byte[] buf)
- In.read (byte[] buf,int start,int size)
4. Basic method of output stream:
-
- Out.write (int b) writes out a byte to stream, B's low 8 bits;
- Out.write (byte[] buf) writes the BUF byte array to the stream;
- Out.write (byte[] buf,int start,int size)
5.FileInputStream---> Specifically read the data on the file, the following see the simplest file read:
PackageCom.wxd.test2;ImportJava.io.FileInputStream;Importjava.io.IOException; Public classIoutil {/*** Read the contents of the specified file, output to the console according to 16 input * and 10 bytes per output *@paramFileName*/ Public Static voidPrinthex (String fileName)throwsIOException {//read the file as a byte streamFileInputStream in =NewFileInputStream (fileName); intb; intI=1; while((B=in.read ())!=-1){ if(b<=0xf){ //number of units front complement 0System.out.print (0); } System.out.print (integer.tohexstring (b)+" "); if(i++%10==0) {System.out.println (); }} in.close (); }}
6.FileOutputStream implements the method of writing byte data to a file:
PackageCom.wxd.test2;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classFileOutDemo1 { Public Static voidMain (string[] args)throwsioexception{//If the file does not exist, it is created directly if it exists after deletion.FileOutputStream out=NewFileOutputStream ("Demo/out.dat"); Out.write (' A ');//write down eight bits of ' A ' characterOut.write (' B ');//write down eight bits of ' B ' character inta=10;//Write only writes low eight bits write an int need to write four times each time 8 bitsOut.write (a>>>24); Out.write (A>>>16); Out.write (A>>>8); Out.write (a); byte[] zg= "China". GetBytes ("UTF-8"); Out.write (ZG); Out.close (); Ioutil.printhex ("Demo/out.dat"); }}
7.dataoutputstream/datainputstream to the "flow" function, it can be more convenient to read Int,long, characters and other types of data:
-
- Writeint ()/writedouble ()/writeutf ()
PackageCom.wxd.test2;ImportJava.io.DataOutputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classDosdemo { Public Static voidMain (string[] args)throwsIOException {String file= "Demo/dos.dat"; DataOutputStream dos=NewDataOutputStream (Newfileoutputstream (file)); Dos.writeint (10); Dos.writeint (-10); Dos.writelong (10l); Dos.writedouble (10.5); //write with UTF-8 codeDOS.WRITEUTF ("China")); //write with UTF-16BE codeDos.writechars ("China")); Dos.close (); }}
PackageCom.wxd.test2;ImportJava.io.DataInputStream;ImportJava.io.FileInputStream;Importjava.io.IOException; Public classDisdemo { Public Static voidMain (string[] args)throwsIOException {String file= "Demo/dos.dat"; DataInputStream Dis=NewDataInputStream (Newfileinputstream (file)); intI=dis.readint ();//actually made 4 reads, because int is 32 bitsSystem.out.println (i); I=Dis.readint (); System.out.println (i); LongL=dis.readlong ();//actually did 8 read, should be a long is 64 bitSystem.out.println (L); DoubleD=dis.readdouble (); System.out.println (d); String s=Dis.readutf (); System.out.println (s); Dis.close (); }}
7.bufferedinputstream&bufferedoutputstrean These two streams provide the IO with a buffer, which is usually buffered when the file is opened for write or read operations, which improves IO performance
Putting data into a file from an application is equivalent to pouring a jar of water into another cylinder:
-
- FileOutputStream--->write () method is equivalent to a drop of water transfer to the past
- DataOutputStream--->writexxx () method will be convenient, the equivalent of a scoop of water transfer past
- Bufferedoutputstream--->write () method is equivalent to a scoop of a ladle first into the bucket, and then poured from the barrel into the cylinder
/*** * Make a copy of the file, using buffered byte stream *@paramSrcfile *@paramDestFile *@throwsIOException*/ Public Static voidCopyfilebybuffer (File srcfile,file destfile)throwsioexception{if(!srcfile.exists ()) { Throw NewIllegalArgumentException ("File:" +srcfile+ "does not exist"); } if(!Srcfile.isfile ()) { Throw NewIllegalArgumentException (srcfile+ "is not a file! "); } Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (srcfile)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (destfile)); intC; while((C=bis.read ())!=-1) {bos.write (c); Bos.flush ();//flushes the buffer, otherwise it cannot be written to;} bis.close (); Bos.close (); }
1. Coding issues
2. Understanding text and text files
3.Java of text (char) is a 16-bit unsigned integer that is a Unicode encoding (double-byte encoding) of a character file that is a byte of byte byte ... The data series
A text file is a sequence of text (char) that is serialized as a storage of byte in accordance with an encoding scheme (UTF-8,UTF-16BE,GBK)
4. Character Stream (Reader Writer):---> operation is a text file
Processing of characters, one character at a time
The bottom of the character is still the basic byte sequence
Basic implementation of character stream
InputStreamReader completes a byte stream parsing to a char stream, parsed by encoding
OutputStreamWriter provides a char stream to a byte stream, which is processed by encoding
PackageCom.wxd.test2;ImportJava.io.*; Public classIsrandoswdemo { Public Static voidMain (string[] args)throwsioexception{//InputStreamReader is called Bridge flowFileInputStream in=NewFileInputStream ("Demo\\test.txt");//The encoding of the default item, the encoding of the file itself to be written when the operationInputStreamReader isr=NewInputStreamReader (In, "Utf-8"); FileOutputStream out=NewFileOutputStream ("Demo\\test2.txt"); OutputStreamWriter OSW=NewOutputStreamWriter (out, "Utf-8");//int C;//While ((C=isr.read ())!=-1) {//System.out.print ((char) c);// } Char[] buffer=New Char[8*1024]; intC; //Bulk Read, put in buffer this character array, starting from the No. 0 position, put up buffe.length//returns the number of characters that are read while((C=isr.read (buffer,0,buffer.length))!=-1) {String s=NewString (buffer,0, c); System.out.println (s); Osw.write (Buffer,0, c); Osw.flush (); } isr.close (); Osw.close (); }}
5.filereader/filewriter (This way bu)
PackageCom.wxd.test2;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classFrandfwdemo { Public Static voidMain (string[] args)throwsioexception{FileReader FR=NewFileReader ("Demo\\test.txt"); FileWriter FW=NewFileWriter ("Demo\\text2.txt",true);//set to True to append later Char[] buffer=New Char[2056]; intC; while((C=fr.read (buffer,0,buffer.length))!=-1) {fw.write (buffer,0, c); Fw.flush (); } fr.close (); Fw.close (); }}
6. Filter for character stream
BufferedReader----->readline read one line at a time
Bufferedwriter/printwriter----> Write a line
PackageCom.wxd.test2;ImportJava.io.*; Public classBrandbworpwdemo { Public Static voidMain (string[] args)throwsIOException {//Read and write to a fileBufferedReader br =NewBufferedReader (NewInputStreamReader (NewFileInputStream ("Demo\\test.txt"))); /*bufferedwriter bw=new BufferedWriter (New OutputStreamWriter (New FileOutputStream ("Demo\\test3.txt"));*/PrintWriter PW=NewPrintWriter ("Demo\\test3.txt"); String Line; while(line = Br.readline ())! =NULL) {System.out.println (line);//reads one line at a time and does not recognize line breaks /*Bw.write (line); Write the line-break operation separately Bw.newline (); Bw.flush ();*/pw.println (line); Pw.flush (); } br.close ();//bw.close ();Pw.close (); }}
Byte stream and character stream commonly used in Java