When I call my BufferedReader my programs freezes

Viewed 42

I am working on a program that displays a object from my Stock class in a jlist. I have it working where I can write this information to a file but when I try to read from that same file my program just freezes. I am wondering if I have my code set up wrong to read from the file as this is my first time trying that. All I am looking to do is set the variables for my Stock object from the saved variables in the file. Thanks

 public void getData(){

    StringTokenizer row;
    Stock aStock = new Stock();

    try{
        BufferedReader inbuffer = new BufferedReader(new FileReader(fileName));
        String inputString;
        inputString = inbuffer.readLine();
        while(inputString != null){
            row = new StringTokenizer(inputString, DELIMTER);
            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);
    }

}
2 Answers

You need an if block instead of a while block because you want to set values to only one Stock object.

Replace

while(inputString != null)

with

if(inputString != null)

Also, change the return type from void to Stock and return aStock so that you can use the returned value.

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

Your loop is not set up correctly, and thus will never exit. Notice that after you read the first line, you are never reading another new line, so the loop never ends.

Secondly, if you are writing to the file at the same time as you are reading from it, inputString will never be null - the call to inbuffer.readLine will wait for more data before returning. You might do better using a threaded callback reader like DataFetcher (https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/io/DataFetcher.java)

    inputString = inbuffer.readLine();
    while(inputString != null){
        row = new StringTokenizer(inputString, DELIMTER);
        aStock.setStockName(row.nextToken());
        aStock.setStockQuantity(Integer.parseInt(row.nextToken()));
        aStock.setPurchasePrice(Double.parseDouble(row.nextToken()));
        aStock.setCurrentPrice(Double.parseDouble(row.nextToken()));
        inputString = inbuffer.readLine();

    }
Related