简体   繁体   English

如何从 JTable txt 文件创建字符串?

[英]How to create Strings from a JTable txt file?

I need to read from a txt file and sort everything in different arrays or strings, allowing me to set text for my JLabels.我需要从一个 txt 文件中读取所有内容,并以不同的 arrays 或字符串对所有内容进行排序,从而允许我为我的 JLabels 设置文本。 One array/string for ID, Item Name, Price and Stock. ID、项目名称、价格和库存的一个数组/字符串。

在此处输入图像描述

This is a preview of my txt file:这是我的 txt 文件的预览:

在此处输入图像描述

Here is my code to read the txt file to import it to my JTable:这是我读取 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();
                }

How do I separate them?我如何将它们分开? Any help would be appreciated.任何帮助,将不胜感激。

TXT FILE:文本文件:

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

It appears that the format separates each of the columns by \ so you can split the String by that.似乎该格式用\分隔每一列,因此您可以以此分割字符串。 What we can do is read all lines from a given Path object which is the Path to the file.我们可以做的是从给定的路径 object 读取所有行,这是文件的路径。 We can then skip the first line which we know is the table column names.然后我们可以跳过我们知道是表列名的第一行。 We can then map each of these lines which are individual String objects to a String array by removing all whitespace with the replaceAll reference and then use the String#split method to split the line by \ which will give us each of the columns for each row.然后,我们可以 map 通过使用 replaceAll 引用删除所有空格来将这些行中的每一行作为字符串数组的单独字符串对象,然后使用 String#split 方法将行拆分为\这将为我们提供每一行的每一列. We can then collect all of these String arrays to a List using the Stream#collect method.然后我们可以使用 Stream#collect 方法将所有这些字符串 arrays 收集到一个列表中。

         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());

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM