集册 Java实例教程 带BufferedReader的Java逐行读取文本文件

带BufferedReader的Java逐行读取文本文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

439
Java使用BufferedReader逐行读取文本文件
/**来自 NowJava.com**/

import java.util.*;

import java.io.*;


public class FileList {

    public static void main(String[] args) {

        final int assumedLineLength = 50;

        File file = new File(args[0]);

        List<String> fileList = new ArrayList<String>(

                (int) (file.length() / assumedLineLength) * 2);

        BufferedReader reader = null;

        int lineCount = 0;

        try {

            reader = new BufferedReader(new FileReader(file));

            for (String line = reader.readLine(); line != null; line = reader

                    .readLine()) {

                fileList.add(line);

                lineCount++;

            }

        } catch (IOException e) {

            System.err.format("Could not read %s: %s%n", file, e);

            System.exit(1);//时代Java公众号 - N o w J a  v a . c o m 提供

        } finally {

            if (reader != null) {

                try {

                    reader.close();

                } catch (IOException e) {

                }

            }

        }

        int repeats = Integer.parseInt(args[1]);

        Random random = new Random();

        for (int i = 0; i < repeats; i++) {

            System.out.format("%d: %s%n", i,

                    fileList.get(random.nextInt(lineCount - 1)));

        }

    }

}


展开阅读全文