java基礎-輸入資料流-讀取文字檔中資料至字串數組

來源:互聯網
上載者:User

標籤:

簡介:如題

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基礎-輸入資料流-讀取文字檔中資料至字串數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.