【问题标题】:Trouble reading data from a file and displaying it in a JTextArea无法从文件中读取数据并将其显示在 JTextArea 中
【发布时间】:2013-08-15 15:33:38
【问题描述】:

这里我只是尝试从保存的文件中读取行,然后将它们显示在 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:这会失败,因为 score 和 score1 尚未在 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。我做错了什么?

【问题讨论】:

    标签: java file-io try-catch


    【解决方案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 {,然后在你完成之后} 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());
    

    【讨论】:

    • 我应该在 try/catch 内还是在外面这样做?
    • 就像在 printStackTrace 位中一样 - 是的。
    • 还有 System.out.println(X.getMessage());
    • 这确实允许在 JTextArea 中显示数据,但它只显示存储的最后一行。我怎样才能让它显示所有的线条?我总共有 10 个。
    • 您只是在阅读 2 行,您只会看到您设置为 setText 替换文本的这两行中的最后一行。
    【解决方案2】:

    我先有几个cmets:

    1) 变量不能以大写开头,这违反了java编码约定,因此难以阅读

    2) 请粘贴堆栈跟踪,以便我们查看到底发生了什么

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

    【讨论】:

    • 大部分内容我都在按照教程进行,并按照显示的方式构建它,所以请告诉我正确的方法。
    • 我将这个建议合并到我的代码中。请问您为什么在尝试之外初始化 BufferedReader ?在第 3 行初始化它是否不正确,如下所示:" BufferedReader reader =new BufferedReader(new FileReader("scores.txt")); "
    • Reader 是在 try 之外声明的,所以你可以在 finally 块中关闭它。您的方法无法使用此模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多