读取.txt文件中的整数并存储在arraylist中

时间:2014-02-19 17:50:16

标签: java arraylist

我知道这个问题可能会多次回答。但是,我仍然无法解决这个具体问题。

基本上我有一个.txt文件,格式如下。

String Integer String

例如,

la 789 ferrari
turbo 560 porsche
veyron 987 bugatti
sls 563 benz
dbs 510 aston

如何逐行读取文件并将数字/整数仅存储到arraylist中?
谢谢!

3 个答案:

答案 0 :(得分:1)

ArrayList<int> list = new ArrayList<int>();

try {

    BufferedReader br = new BufferedReader(new FileReader("file.txt"));
    String line = br.readLine();

    while(line != null)
    {
         String[] tokens = line.Split(" ");
         list.Add(Integer.parseInt(tokens[1]));
         line = br.readLine()

    }
catch(Exception e)
{
    //Probably a conversation exception or a index out of bounds exception
}

答案 1 :(得分:1)

这是一个更完整的Java风格解决方案,使用Java 7 ...为了好玩。

Main.java

import java.util.List;

public class Main
{
    private static final InputFileParser inputFileParser = new InputFileParser();
    private static final EntryNumberExtractor extractor = new EntryNumberExtractor();
    private static final String FILENAME = "input-file.txt";

    public static void main(String... args) 
    {
        List<Entry> entries = inputFileParser.parse(FILENAME);
        List<Integer> extractedIntegers = extractor.extract(entries);

        System.out.println("Entries: ");
        prettyPrintListItems(entries);

        System.out.println();

        System.out.println("Entry numbers: ");
        prettyPrintListItems(extractedIntegers);
    }

    private static <T> void prettyPrintListItems(List<T> list)
    {
        for (T item : list)
        {
            System.out.println(item);
        }
    }
}

InputFileParser.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class InputFileParser
{
    public List<Entry> parse(String filename)
    {
        List<Entry> entries = new ArrayList<>();
        File f = new File(filename);
        try (BufferedReader reader = new BufferedReader(new FileReader(f));)
        {
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                String[] components = line.split(" ");
                entries.add(new Entry(components[0], Integer.parseInt(components[1]), components[2]));
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return entries;
    }
}

EntryNumberExtractor.java

import java.util.ArrayList;
import java.util.List;

public class EntryNumberExtractor
{
    public List<Integer> extract(List<Entry> entries)
    {
        List<Integer> integers = new ArrayList<>();
        for (Entry e : entries) 
        {
            integers.add(e.getNumber());
        }
        return integers;
    }
}

Entry.java

public class Entry
{
    private String model;
    private int number;
    private String company;

    public Entry(String model, int number, String company)
    {
        this.model = model;
        this.number = number;
        this.company = company;
    }

    public Integer getNumber()
    {
        return number;
    }

    @Override
    public String toString() 
    {
        return "model: " + model + ", number: " + number + ", company: " + company;
    }
}

答案 2 :(得分:0)

您可以读取每一行并按空格分割行字符串,检索数字并将其存储在数组列表中