Java--io class, byte stream read data

Source: Internet
Author: User
Tags array length

byte stream read Data inputstream
      ?    FileInputStream;          
The construction method of █ FileInputStream     ? fileinputstream (file file); //Construction object does not exist than required file, it will automatically create file     ? FileInputStream (String name);               
member methods for FileInputStream     ? public int read (); //read Read once will automatically offset to the next byte, return a total read to the byte length, read the end of the file to return 1;     ? public int Read (byte[] b);                   ?        int Read (byte[] b, int off, int len);          
Public class IOTest2 {
Public static void Main (string[] args) throws IOException { FileInputStream fis = new FileInputStream ("1.txt"); //int read = Fis.read (); //File is D, when write (100), written in the file is D read out and become //read Read Only the last byte /*system.out.println ("Read ():" +read); //read (): char ch = (char) read; System.out.println ("Conversion:" +ch); //conversion: D */ byte[] bs = new byte[10]; int readlength = Fis.read (BS); //Returns the actual byte length read; Readone byte at a time is automatically offset to the next String string = new String (BS); //Here constructs a string, the front opens up how much space, even if the array only partially has the data, the conversion time still will be converted by the definition length, no data the empty lattice; //If the front is 10, the resulting string will be more blank than 6, a little longer System.out.println ("Readtobs:" +string+ "Readlength:" +readlength); //readtobs:201703 readlength:6//Array length is 6 //readtobs:201703 readlength:6//Array length is ten String string1 = new string (bs,0,readlength); //Eliminate excess space above System.out.println ("Readtobs:" +string1+ "Readlength:" +readlength);
int length = Fis.read (bs,0,10); //Parameter one indicates to which array to read, parameter two means to write from the position of the array, and the third parameter represents the length String string2 = new string (bs,0,length); when building a string, specify where to start building from the array, and how long it will be built; System.out.println ("Readtobs:" +string2+ "Readlength:" +length); Fis.close (); //Release Resources } }



Public class IOTest3 {
Public static void Main (string[] args) throws IOException { FileInputStream fis = new FileInputStream ("Diguitest.java"); / * int read = 0; while ((read = Fis.read ())! =-1) { //read reads to the end of the file returns-1 char ch = (char) read; System.out.print (CH);                 }*/

//read one byte array at a time //using a byte stream can not handle the text in the Chinese language, will appear garbled; but no garbled characters are built into a string byte[] bytes = new byte[100]; int length = 0; While (length = fis.read (bytes))! =-1) { String string = new string (bytes,0,length); System.out.print (string);                 } fis.close ();         } }






Java--io class, byte stream read data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.