【问题标题】:How to create Strings from a JTable txt file?如何从 JTable txt 文件创建字符串?
【发布时间】: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


【解决方案1】:

该格式似乎用\ 分隔每一列,因此您可以以此分割字符串。我们可以做的是从给定的 Path 对象(即文件的路径)中读取所有行。然后我们可以跳过我们知道是表列名的第一行。然后,我们可以通过使用 replaceAll 引用删除所有空格,将作为单个 String 对象的每一行映射到 String 数组,然后使用 String#split 方法将行拆分为\,这将为我们提供每一列每一行。然后我们可以使用 Stream#collect 方法将所有这些字符串数组收集到一个列表中。

         List<String> lines = Files.readAllLines(Paths.get("first.txt"));

        String[] columnNames = lines.stream().findFirst().orElseThrow(IOException::new).split(",");

        List<MyRow> rows = lines
                .stream()
                .skip(1)
                .map(line -> line.replaceAll(" ", "").split("/"))
                .map(MyRow::valueOf)
                .collect(Collectors.toList());

        DefaultTableModel model3 = new DefaultTableModel();

        model3.setColumnIdentifiers(columnNames);

        rows.forEach(row -> model3.addRow(new Object[] { row.getId(), row.getItemName(), row.getPrice(), row.getStock() }));

        List<Integer> ids = rows.stream().map(MyRow::getId).collect(Collectors.toList());

输出:

[00016, AppleAirpods, 8999, 20]
[00017, SamsungGalaxyBuds, 6999, 13]
[00018, AppleAirpodsPro, 14999, 5]
[00019, BeatsPowerbeatsPro, 13490, 8]
[00020, SonyWF-1000XM3, 10799, 10]

【讨论】:

  • 你能详细说明一下这段代码吗?真的很有帮助,谢谢!
  • 绝对,等一下。
  • 如何将此代码插入现有代码?因为我是 Stream#collect 方法的新手 :(
  • 什么是 DefaultTableModel?如果它是自定义的,请在代码标签的主帖子中提供它。
  • 更新了,试试吧。
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 2021-03-04
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
相关资源
最近更新 更多