StringBuffer獲取txt文本內容_01

將本地的code.txt文件內容存放到StringBuffer變量中:

}

private static final String txtFile="F:/txtFile/code.txt";

   BufferedReader br;

   StringBuffer sb;

  

    @Test

    public void f() throwsIOException {

       try{

        br=newBufferedReader(new InputStreamReader(newFileInputStream(txtFile),"GBK"));

       sb=newStringBuffer();

       Stringstr="";

       while((str=br.readLine())!=null)

       {

          sb.append(str);

          System.out.println(str);

       }

       }catch(Exceptione){

          System.out.println("獲取文本出錯");

       }finally {

         br.close();

      }

}

執行結果:

code:第一行

code:第二行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章