【问题标题】:How to read multiple objects from a text file and then send to an ArrayList?如何从文本文件中读取多个对象,然后发送到 ArrayList?
【发布时间】:2020-06-05 00:50:56
【问题描述】:

我目前让我的程序使用 BufferedReader 读取第一个对象,但我不确定如何读取多个对象。 这是我从文件中读取的代码:

    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”行发送所有对象信息,但我希望“test”行作为出色地。感谢您花时间查看此内容。

【问题讨论】:

    标签: java arraylist bufferedreader


    【解决方案1】:

    您的代码在读取第一行后当前正在停止,因为没有循环继续遍历整个文件。

    您可以使用 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);
    
    }
    

    【讨论】:

    • 哦,是的,我明白你在说什么。但我最大的问题是我什至无法读取文件的第二行以将其发送到该点。
    • 是的,你是对的。我只是匆匆瞥了一眼,并没有注意到。你真的应该从你的 getData() 方法中返回一个股票数组,然后用一个 while 循环遍历所有行。
    • @Zerdall,我做了一些修改。看看,如果您有任何问题,请告诉我。
    • 效果很好!非常感谢,我不确定如何阅读多种内容。我会用你给我的代码练习并习惯它。
    • 太棒了!很高兴我能帮上忙!
    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多