简体   繁体   中英

Trouble reading data from a file and displaying it in a JTextArea

Here I am simply attempting to read lines from a saved file and then display them in a JTextArea.

NOTE: The JTextArea that I am attempting to display to is tested and working properly so the issue is not there.

    try
    {
        File ScoreFile = new File("ScoreFile.FILE"); 
        FileInputStream Read1 = new FileInputStream(ScoreFile);
        InputStreamReader Read2 = new InputStreamReader(Read1); 
        BufferedReader ReadIt = new BufferedReader(Read2);

        String score  = ReadIt.readLine();  
        String score1 = ReadIt.readLine();


        GraphicGameBoard.topScoreDisplay.setText(score  + "\n");
        GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");


        ReadIt.close();
        Read2.close();
        Read1.close();

    }
    catch (Exception X) { System.out.print("Oops, Can't Load.");}

This will catch the exception every time. I have determined that if I remove the attempt to set the text in the topScoreDisplay, it will save the data from the file properly into the score and score1 variables without catching the exception.

I have tried many scenarios and they all fail for a different reason.

1: This fails because score and score1 have not been initialized outside of the try/catch but inside the try/catch the variables are successfully storing the data as the System.out.print shows. If I move the System.out.print outside of the try/catch then it will not print.

   try
    {
        File ScoreFile = new File("ScoreFile.FILE"); 
        FileInputStream Read1 = new FileInputStream(ScoreFile);
        InputStreamReader Read2 = new InputStreamReader(Read1); 
        BufferedReader ReadIt = new BufferedReader(Read2);

        String score  = ReadIt.readLine();  
        String score1 = ReadIt.readLine();

        System.out.print(score + "\n" + score1 + "\n");

        ReadIt.close();
        Read2.close();
        Read1.close();

    }
    catch (Exception X) { System.out.print("Oops, Can't Load.");}

     GraphicGameBoard.topScoreDisplay.setText(score  + "\n");
     GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");

2: If I initialize the variables before the try/catch then the System.out.print will work with the correct information either inside or outside the try/catch. If the .setText is inside, it will catch the exception. If it is outside it will cause a NPE.

         String score;
         String score1;

   try
    {
        File ScoreFile = new File("ScoreFile.FILE"); 
        FileInputStream Read1 = new FileInputStream(ScoreFile);
        InputStreamReader Read2 = new InputStreamReader(Read1); 
        BufferedReader ReadIt = new BufferedReader(Read2);

        score  = ReadIt.readLine();  
        score1 = ReadIt.readLine();

        System.out.print(score + "\n" + score1 + "\n");

        ReadIt.close();
        Read2.close();
        Read1.close();

    }
    catch (Exception X) { System.out.print("Oops, Can't Load.");}

     GraphicGameBoard.topScoreDisplay.setText(score  + "\n");
     GraphicGameBoard.topScoreDisplay.setText(score1 + "\n"); 

So, I can save the file data into the variables and I can get it to display with a System.out.print. But I cannot use the variables to .setText. What am I doing wrong?

You don't say what the exception is X.printStackTrace(); is you friend to find that out.

I'm guessing that this is because you're trying to change the UI from a non-ui thread.

To fix that you'll need to do something like this:

final String score  = ReadIt.readLine();  
final String score1 = ReadIt.readLine();
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        GraphicGameBoard.topScoreDisplay.setText(score  + "\n");
        GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");
    }
});

It's also best to close your streams using a finally block.

So a try { after creating each stream and then } finally { stream.close();} after you're done with it. The reason for this is so that no matter what goes wrong you will clean up the resources you're using.

You might want to read the lines in a loop thus:

final StringBuilder sb = new StringBuilder();
for (String line = readIt.nextLine(); line != null; line = readIt.nextLine()) {
    sb.append(line);
    sb.append("\n");
}

GraphicGameBoard.topScoreDisplay.setText(sb.toString());

I have few comments first:

1) variable shall not start with upper case, it is against java coding conventions and therefore it is hard to read

2) please paste stacktrace, so we can see, what exactly happens

3) choose if yout want to use InputStream or Reader interface. You mix them in strange way. See FileReader javadoc.

followup:

shorter version:

BufferedReader reader = null; 
try { 
    reader = new BufferedReader(new FileReader("scores.txt")); 
    /* Tom's variant */ 
} catch (Exception e) { 
    e.printStackTrace(); 
} finally { 
    if (reader != null) {
        reader.close(); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM