【发布时间】:2020-07-22 04:23:35
【问题描述】:
我需要从一个 txt 文件中读取数据并将所有内容排序到不同的数组或字符串中,以便为我的 JLabels 设置文本。 ID、商品名称、价格和库存的一个数组/字符串。
这是我的 txt 文件的预览:
这是我读取 txt 文件以将其导入我的 JTable 的代码:
String filePath = "C:\\Users\\zagad\\IdeaProjects\\DATABYTES\\stock\\consoles\\consoles.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String firstLine = br.readLine().trim();
String[] columnsName = firstLine.split(", ");
DefaultTableModel model3 = (DefaultTableModel) productTable.getModel();
model3.setColumnIdentifiers(columnsName);
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
model3.addRow(dataRow);
}
} catch (IOException ex) {
ex.printStackTrace();
}
如何将它们分开?任何帮助将不胜感激。
文本文件:
ID , Item Name ,Price , Stock
00016 / Apple Airpods / 8999 / 20
00017 / Samsung Galaxy Buds / 6999 / 13
00018 / Apple Airpods Pro / 14999 / 5
00019 / Beats Powerbeats Pro / 13490 / 8
00020 / Sony WF-1000XM3 / 10799 / 10
【问题讨论】:
-
能否提供文本文件而不是文本文件的图片
-
@Jason 你好!我怎样才能在stockoverflow中做到这一点?我是新来的。谢谢
-
您可以简单地复制您的文本文件并将其放入代码块中,就像您使用其他段一样。
-
@Jason 给你 :)
标签: java arrays string swing jtable