首页 > 解决方案 > 如何修复无法在 JavaFX 中读取的文件

问题描述

我正在开发一个使用 Java FX 的程序,该程序需要能够写入然后读取文件。当我按下 GUI 中的写入按钮时,写入文件工作得非常好,但是,当我尝试从该文件中读取并在 GUI 的文本区域中显示内容时,它会引发我的 catch 异常。我知道有一种更简单的方法来显示文本,但我需要以类似于这种方式的方式来设置各种元素在文本区域中的显示方式。

谢谢!

@FXML
private void read(ActionEvent event) 
{

    ta.clear();
    ta.setText("Name\tDescription\tPrice\t\tCopies\tTotal");
    try
    {
        Scanner input=new Scanner(myfile);

        while(input.hasNext())
        {
            String s=input.next();                
            Scanner sc=new Scanner(s);
            sc.useDelimiter(",");
            String n1=sc.nextLine();
            String d1=sc.nextLine();
            double pr1=sc.nextDouble();
            double cop1=sc.nextDouble();
            double total=pr1*cop1;

            ta.appendText(n1+"\t\t"+d1+"\t\t"+pr1+"\t\t"+cop1+"\n"+total);                                                      
        }
        input.close();

    }
     catch(Exception e)
    {
        System.out.println("Error in reading the file...");
    }

}

我希望 myfile 的内容显示在我的 GUI 的文本区域“ta”中。

标签: javafilejavafxruntime-error

解决方案


推荐阅读