Tag: Data alt uses a read static multiple track ACK RAC
The development of the program has been used to read and write files. But forJavawhere the input stream and output stream are simply not understood, have been trying to figure it out, but have not run (tragedy). Took half an hour to pass this morning .JDK API1.6.0The Chinese version helps you step through the process of reading bytes in the next byte input stream. Here's the word.InputStreamwhichRead (),Read (byte[] b),Read (byte[] b,int off,int len)the use and difference first, read () method:public static void InputStreamRead1 () {try{//constructs an input stream object (read data)Test.txtthe content of the text is: I love you chinaInputStream is = new FileInputStream ("E:\\test.txt"); //constructs an output stream object (write data)OutputStream OS = new FileOutputStream ("E:\\ctest.txt"); Ithe NT len;// represents the data that is read in (in decimal form) FileInputStreambe able to useRead ()method reads one byte at a time and reads the data into theinttype returns,So, if you useSystem.out.println ()when the data is displayed, it isTenin the way of the binary displaywhile (len = Is.read ())!=-1) {System.out.println ("len=" +len); Os.write (len); } os.close (); Is.close (); }catch (Exception e) {e.printstacktrace (); }} to execute the above program, the output of the console is:
Description: I love you china for5a Chinese character,5A Chinese character isTenbytes, and aRead ()method is a byte-by-byte read, so readTentimes. ThenLenIs the value of the Chinese characterTenin-system representation2. Read (byte[] b) method:public static void InputStreamRead2 () {try{//constructing an input stream object (read data)Test.txttext content for multiple I love you chinaInputStream is = new FileInputStream ("E:\\test.txt"); //constructing an output stream object (write data)OutputStream OS = new FileOutputStream ("E:\\ctest.txt"); //Create a length of1024x768of thebyteArrayByte[] B = new byte[1024];//int Len; Indicates the number of bytes successfully read FileInputStreambe able to useRead (byte[] b)read more than one at a time (B.length) byte data (up to the maximum number of reads per time) tobyteArray wherewhile (len = Is.read (b))!=-1) {System.out.println ("len=" +len); Os.write (b); } os.close (); Is.close (); }catch (Exception e) {e.printstacktrace (); The output from the console is as follows:
Note: Maximum reads per1024x768a byte. It is very obvious that such a method is more efficient thanRead ()efficiency of Reading,the process of development is also seen using this method for reading. 3. Read (byte[] b,int off,intlen):1,Public static void InputStreamRead3 () {try{//building the input stream object (read file)InputStream is = new FileInputStream ("E:\\test.txt"); //build the output stream object (write file)OutputStream OS = new FileOutputStream ("E:\\ctest.txt"); //declares an array of size as an input stream sizeByte[] B = new byte[is.available ()];int len = 0;// Indicates the number of bytes successfully read while (Len<is.available ()) {//read (byte[] b,int off, int len)represents the maximum number of entries from the input streamLenbytes of data are read to abyteArray whereLen+=is.read (b, Len, is.available ()-Len); Os.write (b); } os.close (); Is.close (); }catch{E.printstacktrace (); The output of the above program console is:
Note: The one-time read completed 2,Public static void InputStreamRead3 () {try{//building the input stream object (read file)InputStream is = new FileInputStream ("E:\\test.txt"); //build the output stream object (write file)OutputStream OS = new FileOutputStream ("E:\\ctest.txt"); //declares an array of size as an input stream sizeByte[] B = new byte[1024];int len = 0;// Indicates the number of bytes successfully read while ((Len=is.read (b,len,1024))!=-1) {System.out.println ("len=" +len); Os.write (b); } os.close (); Is.close (); }catch{E.printstacktrace (); }} Description: Execute the output of the above control console:
The maximum read1024x768of bytes
|
A little finishing on Java IO InputStream!