標籤:file close [] and import exce buffer public amr
package com.lf.iopreoject;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.InputStreamReader;public class TestCode { public static void main(String[] args) { TestCode.testFileInputStream(); } public static void testFileInputStream() { // 轉換流 File file = new File("song.txt"); FileInputStream inStream = null; BufferedReader br = null; try { inStream = new FileInputStream(file); // 解碼 編碼方式GBK InputStreamReader reader = new InputStreamReader(inStream, "GBK"); br = new BufferedReader(reader); String str = null; while ((str = br.readLine())!=null) { System.out.println(str); } } catch (Exception e) { // TODO: handle exception }finally{ if (br!=null) { try { br.close(); } catch (Exception e2) { // TODO: handle exception } } } } }
Java IO讀取檔案之二