Java Fundamentals-Input stream-reads data from a text file into an array of strings

Source: Internet
Author: User

Introduction: Title

ImportJava.io.FileInputStream;/** * @authorCzchina **/ Public classTeststream { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//declaring a reference to an input streamFileInputStream FLS =NULL; //declaring a reference to the output streamFileOutputStream fos =NULL; Try{            //first, generate an object representing the input streamFLS =NewFileInputStream ("E:/android/androidstudioprojects/text.txt"); //generating a byte array            byte[] buffer=New byte[100]; //call the Read method of the input stream object, read the data (put the read length of the <buffer.length-5> data into the buffer array, 5 is the location to start storing)//Note that the length of the data you read does not exceed the length of the array.             intnum = Fls.read (buffer,5,buffer.length-5); System.out.println ("1, Bytes read from the input stream: \ n" +num);//See how much data has been read from the input streamString S=NewString (buffer); System.out.println ("2, buffer to string, and print: \ n" +s); //The trim method of a String object is called, and the string's leading and trailing spaces are removed and tested as followss =S.trim (); System.out.println ("3, after the string object calls its Trim method, print:\n" +s); }        Catch(Exception e) {System.out.println (e.tostring ()); }    }}

Text.txt

Console

Java Fundamentals-Input stream-reads data from a text file into an array of strings

Related Article

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.