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

Java 如何从文本文件中读取多个对象,然后发送到ArrayList?,java,arraylist,bufferedreader,Java,Arraylist,Bufferedreader,目前,我的程序使用BufferedReader读取第一个对象,但我不确定如何读取多个对象。 以下是我从文件中读取的代码: public Stock getData(){ StringTokenizer row; Stock aStock = new Stock(); try{ BufferedReader inbuffer = new BufferedReader(new FileReader(fileName)); Strin

目前,我的程序使用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”行。感谢您花时间查看此内容。

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

您可以使用while循环检查在循环行时是否有要读取的行:

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

import java.util.ArrayList;
公共ArrayList getData(){
StringTokenizer行;
ArrayList stockList=新的ArrayList();
试一试{
BufferedReader inbuffer=新的BufferedReader(新文件读取器(文件名));
字符串输入字符串;
阿斯托克股票;
//inputString=inbuffer.readLine();
而((line=inbuffer.readLine())!=null){
行=新的StringTokenizer(行,DELIMTER);
if(row.countTokens()==4){
aStock=新股票();
aStock.setStockName(row.nextToken());
aStock.setStockQuantity(Integer.parseInt(row.nextToken());
setPurchasePrice(Double.parseDouble(row.nextToken());
aStock.setCurrentPrice(Double.parseDouble(row.nextToken());
股票列表。添加(aStock);
}
}
inbuffer.close();
}
捕获(ioe异常ioe){
showMessageDialog(null,ioe.getMessage(),“文件读取错误”,JOptionPane.Error);
}
返回库存清单;
}
看起来您还需要在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);

}
public void加载文件(){
StockIO stockRead=新StockIO();
jFileChooser1.showOpenDialog(jPanel3);
File File=jFileChooser1.getSelectedFile();
stockRead.loadFileName(file.getName());
//将所有新股票从getData添加到stockArr
stockArr.addAll(stockRead.getData());
//int指数=0;
对于(int index=0;index
哦,是的,我明白你的意思。但我最大的问题是,我甚至无法阅读文件的第二行来将其发送到该点。是的,你是对的。我只是匆匆瞥了一眼,没有注意到。实际上,您应该从getData()方法返回一个股票数组,并在所有行中循环一个while循环。@Zerdall,我做了一些编辑。看一看,如果你有任何问题,请告诉我。这非常有效!非常感谢你,我不知道如何阅读很多东西。我将用你给我的代码练习并习惯它。太棒了!很高兴我能帮忙!
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);

}