简体   繁体   English

如何从文本文件中读取多个对象,然后发送到 ArrayList?

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

I currently have my program reading the first object using BufferedReader but I am unsure how to read more then one object.我目前让我的程序使用 BufferedReader 读取第一个 object,但我不确定如何读取多个 object。 Here is the code that I have reading from the file:这是我从文件中读取的代码:

    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;
}

The file I am reading from looks like this:我正在读取的文件如下所示: 在此处输入图像描述

and then the section of code that calls to my bufferedReader looks like this:然后调用我的 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);

}

So I am trying to get it where my bufferedReader will read and send both lines of code through.Currently if I run this it will send through the "Shawn" line with all object information but I want the "test" line as well.所以我试图让它在我的 bufferedReader 将读取并发送两行代码的位置。目前,如果我运行它,它将通过“Shawn”行发送所有 object 信息,但我也想要“test”行。 Thank you for your time looking at this.感谢您花时间看这个。

Your code is currently stopping after it reads the first line because there is no loop to continue iterating through your whole file.您的代码在读取第一行后当前正在停止,因为没有循环继续遍历整个文件。

You can use a while loop to check if there is a line to read while looping through your lines:您可以使用 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;
}

It also looks like you also need to add a loop in your loadFile() method to iterate through all of your stocks.看起来您还需要在 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