如何使用文本文件中的数据填充列表?

时间:2016-12-11 14:47:05

标签: java

基本上我有一个打印交易细节的文件。我必须检索帐户ID,金额和日期并将其放入列表中。 到目前为止,我无法检索必要的信息并将其放入列表中。

该文件看起来像:

Transaction on account 00000007: amount 50, dated Tue Dec 06 15:54:35 GMT 2016

所以我必须检索00000007,50和日期并将其放入列表中。这是我被困的地方。

以下是清单:     private List<Transaction> pendingTransactions, completedTransactions;

到目前为止,这是我的代码:

 public void loadPendingTransactions()
{
    try {
        Scanner scnr = new Scanner(new File("pendingTransactions.txt"));
        String content; 

        while((content = scnr.readLine()) != null) {
            String arr[] = content.split(" ");
            int amnt[] = content.split(" ");
            String accID = arr[4];
            int amt = arr[6];
            String date = arr[7];
            pendingTransacrions.add(new Transaction(accID, amt, date));
        }
        scnr.close();

    } catch (FileNotFoundException e) {
        System.out.println("We're sorry, we are unable to find that file: \n" + e.getMessage());
    } catch (IOException e) {
        System.out.println("We're sorry, we are unable to find that file: \n" + e.getMessage());
    }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试逐行读取文件,并将每一行匹配到同一个正则表达式。对于每个匹配,创建一个新的事务对象并将其添加到列表中。

package org.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TransactionLoader {

    public static void main(String[] args) throws Exception {
        String pathToFile = "transactions.txt";
        TransactionLoader loader = new TransactionLoader();
        List<Transaction> transactions = loader.loadTransactions(pathToFile);
        System.out.println("\n\nResults:\n");
        for(Transaction t : transactions) {
            System.out.printf("Transaction ID: %s, amount: %s, date %s%n", t.id, t.amount, t.date);
        }
    }

    private List<Transaction> loadTransactions(String pathToFile) throws Exception {
        Pattern pattern = Pattern.compile("^Transaction on account (\\d+): amount (\\d+), dated (.+)$");
        List<Transaction> transactions = new ArrayList<>();
        try(FileInputStream fis = new FileInputStream(new File(pathToFile));
                InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
                BufferedReader br = new BufferedReader(reader)) {
            String line;
            while((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if(matcher.matches()) {
                    Transaction tx = new Transaction();
                    tx.id = Long.parseLong(matcher.group(1));
                    tx.amount = Integer.parseInt(matcher.group(2));
                    tx.date = new Date(Date.parse(matcher.group(3)));
                    transactions.add(tx);
                } else {
                    System.out.println("Line does not match: " + line);
                }
            }
        }
        return transactions;
    }

    private static class Transaction {
        public long id;
        public int amount;
        public Date date;
    }

}