简体   繁体   中英

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. One array/string for ID, Item Name, Price and Stock.

在此处输入图像描述

This is a preview of my txt file:

在此处输入图像描述

Here is my code to read the txt file to import it to my 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. 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<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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