0

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?

Sev
  • 883
  • 1
  • 14
  • 34

2 Answers2

1

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());
Tom
  • 43,583
  • 4
  • 41
  • 61
  • Should I do that within the try/catch or outside? – Sev Aug 13 '13 at 20:55
  • As in the printStackTrace bit - yup. – Tom Aug 13 '13 at 20:56
  • Also System.out.println(X.getMessage()); – Tom Aug 13 '13 at 20:56
  • That did allow the data to be shown in the JTextArea but it only shows the last line that is stored. How can I get it to show all the lines? I have a total of 10. – Sev Aug 13 '13 at 21:03
  • You're just reading 2 lines, you'll just see the last of those two lines that you set as setText replaces the text. – Tom Aug 13 '13 at 21:07
  • Sorry, didn't mention that this is a stripped version of my full code. What I have for lines 1 and 2 is continued for another 8 lines. Does there exist something in the nature of .addLine() to keep the current line and add another? – Sev Aug 13 '13 at 21:27
  • Answered my own question! http://stackoverflow.com/questions/2088016/add-a-new-line-to-the-end-of-a-jtextarea – Sev Aug 13 '13 at 21:36
1

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(); 
    }
}
Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • For most of this I was following a tutorial and I built it the way it was show, so please let me know the proper way. – Sev Aug 13 '13 at 20:59
  • I am incorporating this advice into my code. May I ask why you initialized the BufferedReader outside of the try? Would it be incorrect to initialize it in line 3 as in: " BufferedReader reader =new BufferedReader(new FileReader("scores.txt")); " – Sev Aug 13 '13 at 21:42
  • Reader is declared outside of try, so you can close it in finally block. Your approach does not enable use of this pattern. – Leos Literak Aug 14 '13 at 03:56