Parse .prn file to html

Viewed 128

I am trying to convert a .prn file to html. But due to file format, I am not able to parse in a way I want. I tried many approaches. some of are:

package main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class PrnToHtml {

public static void main(String[] args) {
    
  try (BufferedReader reader = new BufferedReader(new FileReader(".\\Workbook2.prn"));
             FileWriter writer = new FileWriter("output_prn.html")) {

            writer.write("<html><body><h3>PRN to HTML</h3><table border>\n");

            String currentLine;
            while ((currentLine = reader.readLine()) != null) {
                writer.write("<tr>");

                for(String field: currentLine.split("\\s{2,}"))  // "\\s{2,}"
                    writer.write("<td>" + field + "</td>");

                writer.write("</tr>\n");
            }

            writer.write("</table></body></html>\n");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output of this will be html page looks like this: enter image description here

prn file\data looks like this:

enter image description here

Other this I tried to read this is:

    package main;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URISyntaxException;
    import java.nio.file.Files;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;

    public class PRNToHtml {

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

        public static void main(String[] args) throws URISyntaxException, IOException {
            try (@SuppressWarnings("resource")
            Stream<String> lines = new BufferedReader(new FileReader(".\\Workbook2.prn")).lines()) {
                List<String[]> inputValuesInLines = lines.map(l -> PRN_SPLITTER.split(l)).collect(Collectors.toList());

                
                
                for (String[] strings : inputValuesInLines) {
                    
                    for (String s : strings) {
                        System.out.print(s.replaceAll("\\s+", "") + " ");
                    }
                    System.out.println();
                }
            }
        }
}

output of this is the exactly same looking in prn data file. But when I am trying to embed in html, it is looking weird like this:

enter image description here

Help will be appreciated.

Thank you :)

0 Answers
Related