循环读取文本中每行的数据

java代码:

package com.zltest.bd;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class FileRead {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\aa.txt");
        InputStreamReader read = new InputStreamReader(new FileInputStream(file),"gbk");
        BufferedReader reader = null;
        String temp = null;
        int line = 1;
        try {
            reader = new BufferedReader(read);
            while ((temp = reader.readLine()) != null) {
                System.out.println("line" + line + ":" + temp);
                line++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

文本的内容:

控制台输出的内容:

posted @ 2018-04-24 13:40  岁月淡忘了谁  阅读(335)  评论(0编辑  收藏  举报