java - 如何修复无法在 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”中。
解决方案
推荐阅读
- mysql - 包含变量和 SELECT FROM 的 INSERT INTO 表名
- python - 如何以接近零的成本部署 jupyter notebook 应用程序
- javascript - 承诺:然后在解决之前
- ios - 即使在 info.plist 文件 swift iOS 中添加 NSMicrophoneUsageDescription 后,应用程序也会崩溃
- mysql - SQL 日期
- javascript - 如何在画布圆的小圆弧上添加渐变效果?
- spring - Spring批处理如何编写带有页眉和页脚的XML文件
- java - 以编程方式调用 Spring Security 调用登录
- javascript - 在 Dynamics 365 中使用子网格上的自定义视图进行查找的问题
- linux - 在powershell linux上更改参数的颜色