当我将我的大文本文件存储到arraylist时,大小只有4?

时间:2016-05-11 16:12:37

标签: java arraylist

import java.awt.Graphics;
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundEsxception;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Benford {
    static Object            i    = null;
    static ArrayList<String> list = new ArrayList<String>();
    private static String    data;

    public static void BenfordPercents() {
        int one   = 0;
        int two   = 0;
        int three = 0;
        int four  = 0;
        int five  = 0;
        int six   = 0;
        int seven = 0;
        int eight = 0;
        int nine  = 0;

        return;
    }

    public static void main(String[] args) throws IOException {
        DrawingPanel g       = new DrawingPanel(500, 500);
        Graphics     brush   = g.getGraphics();
        String       popData = null;

        readCount(popData);
        BenfordPercents();
    }

    public static void readCount(String popdata) throws IOException {
        System.out.println("Please make sure the data file is name popData.txt");
        System.out.println("We are loading popData.txt...");

        Scanner console = new Scanner(System.in);

        // Scanner console = new Scanner((new File("popData.txt")));

        try {
            i = new FileInputStream("popData.txt");
        } catch (FileNotFoundException e) {
            System.out.println("We cannot locate popData.txt");
            System.out.println("Please make sure popData.txt is in the same location" + " as your code file!");

            return;
        }

        System.out.println("popData.txt has loaded!");
        System.out.println("");
        System.out.println("Please press enter to show data!");
        data = console.nextLine();

        File            file   = new File(popdata + ".txt");
        FileInputStream fis    = new FileInputStream("popdata.txt");
        byte[]          flush  = new byte[1024];
        int             length = 0;

        while ((length = fis.read(flush)) != -1) {
            list.add(new String(flush));

            // System.out.println(new String(flush));
            // System.out.println(list.toString());
        }

        // Scanner x = new Scanner((new File("popData.txt")));
        // while(x.hasNext()){
        // list.add(x.next());
        // }
        fis.close();
    }
}

我有一个文本文档,其中包含此链接中的所有人数: https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population

文本文档如下所示:

China   1369480000
India   1270250000
United  320865000
Indonesia   255461700
Brazil  204215000
Pakistan    189607000
..etc
..etc

列表很长。

当我尝试将所有这些数字存储到数组列表中时,我尝试打印数组列表大小,它只返回4?

1 个答案:

答案 0 :(得分:0)

如果你想要每行的列表条目,你应该更喜欢BufferedReader。

FileInputStream fis = new FileInputStream("popdata.txt")
BufferedReader br = new BufferedReader(new InputStreamReader(fis));

while((String line = br.readLine()) !=null) {
    list.add(line);
}

4的大小只是你的arraylist的大小 - 数组列表的每个元素都是从一个长度为1024的字节数组构建的。这意味着你的列表要么包含大约3K直到4K的数据。