簡體   English   中英

如何從文本文件中讀取多個對象,然后發送到 ArrayList?

[英]How to read multiple objects from a text file and then send to an ArrayList?

我目前讓我的程序使用 BufferedReader 讀取第一個 object,但我不確定如何讀取多個 object。 這是我從文件中讀取的代碼:

    public Stock getData(){

    StringTokenizer row;
    Stock aStock = new Stock();

    try{
        BufferedReader inbuffer = new BufferedReader(new FileReader(fileName));
        String inputString;
        inputString = inbuffer.readLine();
        if(inputString != null){
            row = new StringTokenizer(inputString, DELIMTER);
            if(row.countTokens() == 4){
                aStock.setStockName(row.nextToken());
                aStock.setStockQuantity(Integer.parseInt(row.nextToken()));
                aStock.setPurchasePrice(Double.parseDouble(row.nextToken()));
                aStock.setCurrentPrice(Double.parseDouble(row.nextToken()));

            }
        }
        inbuffer.close();
    }
    catch(IOException ioe){
        JOptionPane.showMessageDialog(null, ioe.getMessage(), "File Read Error", JOptionPane.ERROR);
    }
    return aStock;
}

我正在讀取的文件如下所示: 在此處輸入圖像描述

然后調用我的 bufferedReader 的代碼部分如下所示:

    public void loadFile(){
    StockIO stockRead = new StockIO();
    jFileChooser1.showOpenDialog(jPanel3);
    File file = jFileChooser1.getSelectedFile();
    stockRead.loadFileName(file.getName());
    stockArr.add(stockRead.getData());

    int index = 0;
    if(stockArr.get(index) != null){
        DLM.addElement(stockArr.get(0).getStockName());
        index ++;
    }


    listStock.setModel(DLM);

}

所以我試圖讓它在我的 bufferedReader 將讀取並發送兩行代碼的位置。目前,如果我運行它,它將通過“Shawn”行發送所有 object 信息,但我也想要“test”行。 感謝您花時間看這個。

您的代碼在讀取第一行后當前正在停止,因為沒有循環繼續遍歷整個文件。

您可以使用 while 循環來檢查在循環遍歷行時是否有要讀取的行:

while ((line = inbuffer.readLine()) != null) {
   // Process each line
}

import java.util.ArrayList;

public ArrayList<Stock> getData(){

    StringTokenizer row;

    ArrayList<Stock> stockList = new ArrayList<>();

    try{
        BufferedReader inbuffer = new BufferedReader(new FileReader(fileName));
        String inputString;
        Stock aStock;
        // inputString = inbuffer.readLine();
        while ((line = inbuffer.readLine()) != null){
            row = new StringTokenizer(line, DELIMTER);
            if(row.countTokens() == 4){
                aStock = new Stock();
                aStock.setStockName(row.nextToken());
                aStock.setStockQuantity(Integer.parseInt(row.nextToken()));
                aStock.setPurchasePrice(Double.parseDouble(row.nextToken()));
                aStock.setCurrentPrice(Double.parseDouble(row.nextToken()));
                stockList.add(aStock);
            }
        }
        inbuffer.close();
    }
    catch(IOException ioe){
        JOptionPane.showMessageDialog(null, ioe.getMessage(), "File Read Error", JOptionPane.ERROR);
    }

    return stockList;
}

看起來您還需要在 loadFile() 方法中添加一個循環來遍歷所有股票。

public void loadFile(){
    StockIO stockRead = new StockIO();
    jFileChooser1.showOpenDialog(jPanel3);
    File file = jFileChooser1.getSelectedFile();
    stockRead.loadFileName(file.getName());

    // Add all new stocks from getData to stockArr
    stockArr.addAll(stockRead.getData());

    // int index = 0;
    for (int index = 0; index < stockArr.length; index++) {
        if(stockArr.get(index) != null){
            DLM.addElement(stockArr.get(index).getStockName());
        }
    }

    listStock.setModel(DLM);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM