首页 > 代码库 > Java IO读取文件之二

Java IO读取文件之二

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读取文件之二