Then the previous article http://www.cnblogs.com/oumyye/p/4314412.html
Java I/O stream---memory operation Flow bytearrayinputstream and Bytearrayoutputstream
The bytearrayinputstream contains an internal buffer that contains bytes read from the stream, and the internal counter follows the next byte to be provided by the Read method.
FileInputStream is to treat files as data sources. Bytearrayinputstream is a data source for an array in memory.
The Bytearrayoutputstream class implements an output stream. Where the data is written to a byte array. The buffer grows automatically as data is written continuously. You can use Tobytearray () and ToString () to get the data source. Bytearrayoutputstream points an output stream to a byte array, but this byte array is built inside the bytearrayoutputstream and does not need to be defined.
Operation Steps
ImportJava.io.* ; Public classbytearraydemo01{ Public Static voidMain (String args[]) {string str= "HELLOWORLD";//defines a string, all of which consist of uppercase lettersBytearrayinputstream bis =NULL;//Memory Input StreamBytearrayoutputstream BOS =NULL;//Memory output Streambis =NewBytearrayinputstream (Str.getbytes ());//output content to memoryBOS =NewBytearrayoutputstream ();//preparing to read content from memory Bytearrayinputstream inttemp = 0 ; while((Temp=bis.read ())!=-1){ Charc = (Char) temp;//the number read becomes a characterBos.write (Character.tolowercase (c));//Change a character to lowercase } //all the data is in the Bytearrayoutputstream .String newstr = bos.tostring ();//Remove Content Try{bis.close (); Bos.close (); }Catch(IOException e) {e.printstacktrace (); } System.out.println (NEWSTR); }};
Java I/O stream---Filter stream-buffered streams Bufferedinputstream and Bufferedoutputstream
The class Bufferedinputstream and Bufferedoutputstream inherit FilterInputStream and Filteroutputstream, implementing a buffered filter stream, which provides a buffering mechanism that takes any I/O stream " Bundle to the buffer stream, you can increase the read efficiency of the I/O stream, and at initialization, you can specify the size of the buffer, in addition to specifying the I/O stream to which I am connected.
While reading and writing to the data cache, so that each read and write data to do the actual physical read and write operations, when using Bufferdoutputstream output, the data input buffer, when the buffer is full and then write to the connected output stream, you can call flush () to clear the buffer. These two streams are process streams, which increase the efficiency of the flow of operations through an internal cache array
using Bufferedinputstream and Bufferedoutputstream to implement file replicationvoidcopyFile (String src, String dec) {FileInputStream fis=NULL; Bufferedinuptstream bis=NULL; FileOutputStream Fos=NULL; Bufferedoutputstream Bos=NULL; Try{FIS=NewFileInputStream (SRC); FOS=NewFileOutputStream (DEC); Bis=NewBufferedinputstream (FIS); Bos=NewBufferedoutputstream (FOS); while((Temp=bis.read ())!=-1) {bos.write (temp); } } Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(IOException e) {e.printstacktrace (); }finally { //after you increase the processing flow, note the closing order of the stream! "Open first Close" Try{bos.close (); } Catch(IOException e) {e.printstacktrace (); } Try{bis.close (); } Catch(IOException e) {e.printstacktrace (); } Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); } }}
java I/O---data manipulation flow datainputstream and DataOutputStream
Data manipulation flows are often written or read in a stream a result is used, such as network data passing, DataInputStream and DataOutputStream provide access to all Java-based types of data that are not machine-independent.
Instance Data write
ImportJava.io.DataOutputStream;ImportJava.io.File;ImportJava.io.FileOutputStream; Public classdataoutputstreamdemo{ Public Static voidMain (String args[])throwsexception{//all exceptions ThrownDataOutputStream dos =NULL;//declaring a data output stream objectFile f =NewFile ("D:" + File.separator + "Order.txt");//save path to fileDOS =NewDataOutputStream (NewFileOutputStream (f));//instantiating a data output stream objectString names[] = {"Shirt", "glove", "scarf"};//Product Name floatPrices[] = {98.3f,30.3f,50.5f};//Commodity price intNums[] = {3,2,1};//Number of goods for(inti=0;i<names.length;i++) {//Loop OutputDos.writechars (Names[i]);//Write StringDos.writechar (' \ t ');//Write delimiterDos.writefloat (Prices[i]);//Write PriceDos.writechar (' \ t ');//Write delimiterDos.writeint (Nums[i]);//Number of writesDos.writechar (' \ n ');//line Break} dos.close (); //turn off the output stream }};
Data written using DataOutputStream read in with DataInputStream
ImportJava.io.DataInputStream;ImportJava.io.File;ImportJava.io.FileInputStream; Public classdatainputstreamdemo{ Public Static voidMain (String args[])throwsexception{//all exceptions ThrownDataInputStream dis =NULL;//declaring data input stream objectsFile f =NewFile ("D:" + File.separator + "Order.txt");//save path to filedis =NewDataInputStream (NewFileInputStream (f));//instantiating data input stream objectsString name =NULL;//Receive name floatPrice = 0.0f;//Receive Price intnum = 0;//Received Quantity CharTemp[] =NULL;//Receive product name intlen = 0;//Save the number of read data Charc = 0;//' \u0000 ' Try{ while(true) {Temp=New Char[200];//Open SpaceLen = 0 ; while((C=dis.readchar ())! = ' \ t ') {//Receive contentTemp[len] =C; Len++ ;//read length plus 1} name=NewString (Temp,0,len);//Change a character array to a stringPrice = Dis.readfloat ();//Read PriceDis.readchar ();//read \ tnum = Dis.readint ();//Read intDis.readchar ();//read \ nSystem.out.printf ("Name:%s; price:%5.2f; Quantity:%d\n", Name,price,num); } }Catch(Exception e) {} dis.close (); }};
java I/o---print stream
In the entire IO package, the print stream is the most convenient class for outputting information, mainly containing the byte print stream (PrintStream) and the character print stream (PrintWriter). The print stream provides a very convenient printing function that can print any data type.
Print input
ImportJava.io.* ; Public classprintdemo02{ Public Static voidMain (String arg[])throwsexception{PrintStream PS=NULL;//declaring a print stream object//If you are using Fileouputstream instantiation, it means that all output is to the filePS =NewPrintStream (NewFileOutputStream (NewFile ("D:" + File.separator + "Test.txt"))) ; String name= "Even my yes";//Defining Strings intAge = 20;//defining integers floatScore = 990.356f;//Defining decimals CharSex = ' M ';//Defining charactersps.printf ("Name:%s; age:%d; score:%f; Gender:%c", Name,age,score,sex); Ps.close (); }};
Bufferreader
Bufferreader reads the contents from the buffer, and all input byte data is placed in the buffer.
ImportJava.io.* ; Public classbufferedreaderdemo01{ Public Static voidMain (String args[]) {BufferedReader buf=NULL;//declaring ObjectsBUF =NewBufferedReader (NewInputStreamReader (system.in));//Convert a stream of bytes into a character streamString str =NULL;//Receive input contentSystem.out.print ("Please enter content:") ; Try{str= Buf.readline ();//reading a row of data}Catch(IOException e) {e.printstacktrace (); //Output Information} System.out.println ("The input content is:" +str); }};
Java Fundamentals---I/O technology (ii)