无法从文件中读取数据并将其显示在JTextArea中

时间:2013-08-13 20:45:49

标签: java file-io try-catch

这里我只是尝试从保存的文件中读取行,然后在JTextArea中显示它们。

注意:我试图显示的JTextArea已经过测试并正常工作,因此不存在问题。

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

每次都会捕获异常。我已经确定,如果我删除了在topScoreDisplay中设置文本的尝试,它会将文件中的数据正确保存到score和score1变量中,而不会捕获异常。

我尝试了很多场景,但由于其他原因,它们都失败了。

1:这会失败,因为得分和得分1尚未在try / catch之外初始化,但在try / catch中,变量成功存储数据,如System.out.print所示。如果我将System.out.print移到try / catch之外,则它将无法打印。

   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:如果我在try / catch之前初始化变量,那么System.out.print将在try / catch内部或外部使用正确的信息。如果.setText在里面,它将捕获异常。如果它在外面会导致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"); 

因此,我可以将文件数据保存到变量中,然后我可以使用System.out.print显示它。但我不能将变量用于.setText。我做错了什么?

2 个答案:

答案 0 :(得分:1)

你不能说出异常是什么X.printStackTrace();你是朋友找到的。

我猜这是因为您正在尝试从非ui线程更改UI。

要解决此问题,您需要执行以下操作:

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

最好使用finally块关闭你的流。

创建每个流后再try {,然后{I}完成后再} finally { stream.close();}。这样做的原因是,无论出现什么问题,您都会清理您正在使用的资源。

您可能希望读取循环中的行:

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

答案 1 :(得分:1)

我先发表评论:

1)变量不能以大写字母开头,它是针对java编码约定的,因此很难读取

2)请粘贴stacktrace,这样我们就可以看到,究竟发生了什么

3)选择是否要使用InputStream或Reader接口。你以奇怪的方式混合它们。请参阅FileReader javadoc。

随访:

更短的版本:

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