Home > database >  Get column width to pass as chunks to parse prn file
Get column width to pass as chunks to parse prn file

Time:06-23

I have prn file which I need to parse. I am able to parse it by using chunks. But I am passing chunk size (Column width) manually. I want to pass that dynamically. Approach I am looking for is to read header line and get the column width of each column. which will be integer array. Any other approach or optimization will be appriciated.

My code is:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    public class PrnParser {

        public void parsePrn(String inputReader, String outputWriter) {

            try (BufferedReader br = new BufferedReader(new FileReader("..//data.prn"))); {
                String line;
                while ((line = br.readLine()) != null) {

                    if (line.trim().equals("")) {
                        continue;
                    }
                    // Method called with supplied file data line and the widths of
                    // each column as outlined within the file.

                    String[] parts = splitStringToChunks(line, 16, 22, 9, 14, 13, 8);
                    for (String str : parts) {
                        System.out.println(str);
                    }
                    System.out.println();
                }

            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }

        public String[] splitStringToChunks(String inputString, Integer... chunkSizes) {
            List<String> list = new ArrayList<>();
            int chunkStart = 0, chunkEnd = 0;
            for (int length : chunkSizes) {
                chunkStart = chunkEnd;
                chunkEnd = chunkStart   length;
                String dataChunk = inputString.substring(chunkStart, chunkEnd);
                list.add(dataChunk.trim());
            }
            return list.toArray(new String[0]);
        }

    }

Here in this code I am using chunk sizes manually as argument of splitStringToChunks function

    String[] parts = splitStringToChunks(line, 16, 22, 9, 14, 13, 8);

A file can have more or less columns so I need that dynamically.

Adding data.prn file link.

https://drive.google.com/file/d/19VU29P27H-Hx4wkcHobtI84xOK3AKEHL/view?usp=sharing

CodePudding user response:

Have a look to below code:

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {

    private static final String DILIM_PRN = " ";
    private static final Pattern PRN_SPLITTER = Pattern.compile(DILIM_PRN);

    private static final String PAD_COLUMN = "pad";

    public static void main(String[] args) {
        String data = "Name            Address               Postcode Phone         Credit_Limit Birthday";
        data = data   PAD_COLUMN;

        List<String> columnList = Arrays.stream(PRN_SPLITTER.split(data)).filter(c -> !c.isEmpty()).collect(Collectors.toList());
        columnList.add(PAD_COLUMN);

        List<Integer> tt = columnList.stream().map(data::indexOf).collect(Collectors.toList());
        IntStream.range(0, tt.size() - 1).map(i -> tt.get(i 1) - tt.get(i)).forEach(System.out::println);

        //this gives output as 16, 22, 9, 14, 13, 8
    }
}

CodePudding user response:

This article can help to get the Java: Infer Column Widths of a Fixed-Width Text File https://dev.to/awwsmm/java-infer-column-widths-of-a-fixed-width-text-file-2hh0

  • Related