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