码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO读取文件之二

时间:2016-11-21 19:01:51      阅读:233      评论:0      收藏:0      [点我收藏+]

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

标签:file   close   []   and   import   exce   buffer   public   amr   

原文地址:http://www.cnblogs.com/lantu1989/p/6085930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!