问题描述
我正在尝试将数据保存在 Students.txt
中(在一个 JFrame
中),然后在另一个框架中的 JTable
(根据百分比 - 从高到低)中打开它。
这是我编写文本文件的代码(它确实可以)
我还想补充的唯一一件事是,如果其中一个 txt 输入为空或者 txtInputMark
不是整数值,它会显示一条错误消息。
package hpxs200.pkg1;
import javax.swing.JTextField;
import java.io.File;
import javax.swing.JOptionPane;
import java.io.FileWriter;
import java.io.IOException;
public class frmRegistration extends javax.swing.JFrame {
String strInput1;
String strInput2;
String strInput3;
Integer intInput1;
File Textfile = new File("Student.txt");
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
strInput1 = txtInputFName.getText();
strInput2 = txtInputLName.getText();
strInput3 = txtInputMark.getText();
try
{
Textfile.createNewFile();
lblOutput1.setText("Student information has been saved");
}
catch (Exception Ex)
{
JOptionPane.showMessageDialog(null,Ex);
}
try
{
String strfilename = "Student.txt";
FileWriter fw = new FileWriter(strfilename,true);
fw.write(strInput1 + ";");
fw.write(strInput2 + ";");
fw.write(strInput3 + "\n");
fw.close();
}
catch (IOException | NumberFormatException Ex)
{
JOptionPane.showMessageDialog( null,Ex);
}
}
private void txtInputMarkActionPerformed(java.awt.event.ActionEvent evt) {
// Todo add your handling code here:
}
private void btnClearactionPerformed(java.awt.event.ActionEvent evt) {
txtInputFName.setText("");
txtInputLName.setText("");
txtInputMark.setText("");'''
这就是我检索数据的全部内容,我不知道该怎么做。
我希望将文本文件中的数据显示在表格中,从最高标记到最低标记,请记住数据文件是在另一个框架中创建的。
private void btndisplayStudentInfoActionPerformed(ActionEvent evt) {
String strInput4;
BufferedReader brReader = null;
InputStreamReader isReader = null;
try{
try (FileInputStream fisFin = new FileInputStream("Students.txt");
InputStreamReader isrReader = new InputStreamReader(fisFin))
{
brReader = newBufferedReader(isrReader);
while((strInput4 = brReader.readLine()) !=null)
{
lblOutput.setText("content has been read");
}
brReader.close();
}
}
catch (Exception Ex)
{
JOptionPane.showMessageDialog(null,Ex);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)