代码改变世界

Java IO读取文件之二

2016-11-21 16:33  甘雨路  阅读(149)  评论(0编辑  收藏  举报
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
                }
                
            }
        }
        
        
    }

    
}