如何读取 txt 文件的特定行并显示到 JTable
How to read specific lines of a txt file and display to JTable
我正在尝试将 txt 文件显示到我的 JTable
中。目的是显示用户名和帐户类型。我设置了一个 JComboBox
,它有 Admins
和 Cashiers
选项。
txt 文件用于登录屏幕,它存储在一个 txt 文件中。这仅适用于学校项目。
admin.txt
的 txt 文件内容:
Username:admin
Password:password
Type:admin
Username:admin2
Password:password2
Type:admin
输出:
这是代码:
String filePath = "C:\Users\zagad\IdeaProjects\DATABYTES\login\admin.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
DefaultTableModel model = (DefaultTableModel) userTable.getModel();
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < 7; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.trim().split(" ");
model.addRow(dataRow);
}
} catch (IOException ex) {
ex.printStackTrace();
}
我想要的是隐藏文本“Username: , Password: and Type:
”。我只想在左侧显示用户名,在右侧显示帐户类型,即admin。
我不确定如何编写代码来拆分它并将它们显示到它们的后续 JTable
单元格中。
而且,用户将添加更多帐户,因此列表会更长,格式相同。
如有任何帮助,我们将不胜感激!
预期输出:
尝试代码后:
您需要解析文件中的数据。
一种方法可能是为每对数据创建 HashMap
。
然后当您阅读文件中的每一行时,您会:
- 将数据分成两个字段
- 将数据添加到 HashMap
- 当您在 HashMap 中有 3 个项目时,现在是将数据添加到 TableModel 的时候了。然后你清空HashMap。
所以基本结构可能是这样的:
HashMap<String, String> userInfo = new HashMap<String, String>();
//for (int i = 0; i < 7; i++) // don't make assumptions about the size
for (int i = 0; i < tableLines.length; i++)
{
String line = tableLines[i].toString().trim();
String[] dataRow = line.trim().split(" "); // why are you splitting on " "???
userInfo.put(dataRow[0], dataRow[1]);
if (userInfo.size() == 3)
{
Vector<String> row = new Vector<String>();
row.add( userInfo.get("Username") );
row.add( userInfo.get("Type") );
model.addRow( row );
userInfo.clear();
}
}
当然,此解决方案假定数据始终采用正确的格式。您还需要空白行的错误检查代码,因为 split(…)
方法不会 return 任何数据。
我正在尝试将 txt 文件显示到我的 JTable
中。目的是显示用户名和帐户类型。我设置了一个 JComboBox
,它有 Admins
和 Cashiers
选项。
txt 文件用于登录屏幕,它存储在一个 txt 文件中。这仅适用于学校项目。
admin.txt
的 txt 文件内容:
Username:admin
Password:password
Type:admin
Username:admin2
Password:password2
Type:admin
输出:
这是代码:
String filePath = "C:\Users\zagad\IdeaProjects\DATABYTES\login\admin.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
DefaultTableModel model = (DefaultTableModel) userTable.getModel();
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < 7; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.trim().split(" ");
model.addRow(dataRow);
}
} catch (IOException ex) {
ex.printStackTrace();
}
我想要的是隐藏文本“Username: , Password: and Type:
”。我只想在左侧显示用户名,在右侧显示帐户类型,即admin。
我不确定如何编写代码来拆分它并将它们显示到它们的后续 JTable
单元格中。
而且,用户将添加更多帐户,因此列表会更长,格式相同。
如有任何帮助,我们将不胜感激!
预期输出:
尝试代码后:
您需要解析文件中的数据。
一种方法可能是为每对数据创建 HashMap
。
然后当您阅读文件中的每一行时,您会:
- 将数据分成两个字段
- 将数据添加到 HashMap
- 当您在 HashMap 中有 3 个项目时,现在是将数据添加到 TableModel 的时候了。然后你清空HashMap。
所以基本结构可能是这样的:
HashMap<String, String> userInfo = new HashMap<String, String>();
//for (int i = 0; i < 7; i++) // don't make assumptions about the size
for (int i = 0; i < tableLines.length; i++)
{
String line = tableLines[i].toString().trim();
String[] dataRow = line.trim().split(" "); // why are you splitting on " "???
userInfo.put(dataRow[0], dataRow[1]);
if (userInfo.size() == 3)
{
Vector<String> row = new Vector<String>();
row.add( userInfo.get("Username") );
row.add( userInfo.get("Type") );
model.addRow( row );
userInfo.clear();
}
}
当然,此解决方案假定数据始终采用正确的格式。您还需要空白行的错误检查代码,因为 split(…)
方法不会 return 任何数据。