標籤:
簡介:如題
import java.io.FileInputStream;/** * @author czchina * */public class TestStream { public static void main(String[] args) { // TODO Auto-generated method stub //聲明輸入資料流的引用 FileInputStream fls = null; //聲明輸出資料流的引用 FileOutputStream fos =null; try{ //一、產生代表輸入資料流的對象 fls = new FileInputStream("E:/Android/AndroidStudioProjects/text.txt"); //產生一個位元組數組 byte [] buffer= new byte [100]; //調用輸入資料流對象的read方法,讀取資料(將讀出來的長度為<buffer.length-5>的資料放入buffer數組中,5是開始存放的位置) //注意讀出來的資料長度不要超過數組長度。 int num = fls.read(buffer,5,buffer.length-5); System.out.println("1、從輸入資料流讀出的位元組數:\n"+num);//看看從輸入資料流中讀取了多少資料 String s = new String(buffer); System.out.println("2、buffer to string, and print: \n"+s); //調用一個String對象的trim方法,會去掉字串的首尾空格,測試如下 s = s.trim(); System.out.println("3、String對象調用其trim方法後,print:\n"+s); } catch(Exception e){ System.out.println(e.toString()); } }}
text.txt
console:
java基礎-輸入資料流-讀取文字檔中資料至字串數組