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

huangapple go评论85阅读模式
英文:

How to create Strings from a JTable txt file?

问题

以下是翻译好的部分:

我需要从一个txt文件中读取内容,并将所有内容按照不同的数组或字符串进行排序,以便我可以为我的JLabel设置文本。一个用于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();
}

如何分隔它们?任何帮助将不胜感激。

TXT文件:

ID      , 商品名称                ,价格      , 库存
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
英文:

I need to read from a txt file and sort everything in different arrays or strings, allowing me to set text for my JLabels. One array/string for ID, Item Name, Price and Stock.

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

This is a preview of my txt file:

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

Here is my code to read the txt file to import it to my JTable:

String filePath = &quot;C:\\Users\\zagad\\IdeaProjects\\DATABYTES\\stock\\consoles\\consoles.txt&quot;;
                File file = new File(filePath);
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String firstLine = br.readLine().trim();
                    String[] columnsName = firstLine.split(&quot;, &quot;);

                    DefaultTableModel model3 = (DefaultTableModel) productTable.getModel();
                    model3.setColumnIdentifiers(columnsName);

                    Object[] tableLines = br.lines().toArray();

                    for (int i = 0; i &lt; tableLines.length; i++) {
                        String line = tableLines[i].toString().trim();
                        String[] dataRow = line.split(&quot;/&quot;);
                       
                        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

答案1

得分: 0

以下是翻译好的代码部分:

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]
英文:

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. 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. We can then collect all of these String arrays to a List using the Stream#collect method.

         List&lt;String&gt; lines = Files.readAllLines(Paths.get(&quot;first.txt&quot;));

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

        List&lt;MyRow&gt; rows = lines
                .stream()
                .skip(1)
                .map(line -&gt; line.replaceAll(&quot; &quot;, &quot;&quot;).split(&quot;/&quot;))
                .map(MyRow::valueOf)
                .collect(Collectors.toList());

        DefaultTableModel model3 = new DefaultTableModel();

        model3.setColumnIdentifiers(columnNames);

        rows.forEach(row -&gt; model3.addRow(new Object[] { row.getId(), row.getItemName(), row.getPrice(), row.getStock() }));
        
        List&lt;Integer&gt; ids = rows.stream().map(MyRow::getId).collect(Collectors.toList());

Output:

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

huangapple
  • 本文由 发表于 2020年4月9日 22:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61123021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定