简体   繁体   English

在Java中将.prn文件转换为CSV文件格式

[英]Convert .prn file to csv file format in java

need your help to convert prn file to csv file using java. 需要您的帮助,以使用Java将prn文件转换为csv文件。 Thank you so much. 非常感谢。

Below is my prn file. 下面是我的prn文件。

i would like to make it shows like this 我想这样显示 在此处输入图片说明

Thank you so much. 非常感谢。

In your example you have four entries as input, each in a row. 在您的示例中,您有四个条目作为输入,每行一个。 In your result table they all are in one row. 在结果表中,它们全部都位于一行中。 I assume the input describes a complete prn set. 我假设输入描述了完整的prn集。 So if a file would contain n prn sets, it would have n * 4 rows. 因此,如果一个文件包含n prn集,则它将具有n * 4行。

To map the pm set to a csv file you have to 要将pm集映射到csv文件,您必须

  1. read in the entries from the input file 从输入文件中读取条目
  2. write a header row (with eight titles) 编写标题行(包含八个标题)
  3. extract in each entry the relevant values 在每个条目中提取相关值
  4. combine the extracted values from four entries in sequence to one csv row 将从四个条目中依次提取的值组合到一个csv行中
  5. write the row 写行
  6. repeat steps 3 to 5 as long as there are further entries 只要还有其他条目,请重复步骤3至5

Here is my suggestion: 这是我的建议:

public class PrnToCsv {

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

    public static void main(String[] args) throws URISyntaxException, IOException {
        List<String> inputLines = Files.readAllLines(new File("C://Temp//csv/input.prn").toPath());
        List<String[]> inputValuesInLines = inputLines.stream().map(l -> PRN_SPLITTER.split(l)).collect(Collectors.toList());

        try (BufferedWriter bw = Files.newBufferedWriter(new File("C://Temp//csv//output.csv").toPath())) {
            // header
            bw.append("POL1").append(DILIM_CSV).append("POL1_Time").append(DILIM_CSV).append("OLV1").append(DILIM_CSV).append("OLV1_Time").append(DILIM_CSV);
            bw.append("POL2").append(DILIM_CSV).append("POL2_Time").append(DILIM_CSV).append("OLV2").append(DILIM_CSV).append("OLV2_Time");
            bw.newLine();

            // data
            for (int i = 0; i + 3 < inputValuesInLines.size(); i = i + 4) {
                String[] firstValues = inputValuesInLines.get(i);
                bw.append(getId(firstValues)).append(DILIM_CSV).append(getDateTime(firstValues)).append(DILIM_CSV);
                String[] secondValues = inputValuesInLines.get(i + 1);
                bw.append(getId(secondValues)).append(DILIM_CSV).append(getDateTime(secondValues)).append(DILIM_CSV);
                String[] thirdValues = inputValuesInLines.get(i + 2);
                bw.append(getId(thirdValues)).append(DILIM_CSV).append(getDateTime(thirdValues)).append(DILIM_CSV);
                String[] fourthValues = inputValuesInLines.get(i + 3);
                bw.append(getId(fourthValues)).append(DILIM_CSV).append(getDateTime(fourthValues));
                bw.newLine();
            }
        }
    }

    public static String getId(String[] values) {
        return values[1];
    }

    public static String getDateTime(String[] values) {
        return values[2] + " " + values[3];
    }

}

Some remarks to the code: 对代码的一些说明:

  • Using the nio-API you can read the whole file with one line of code. 使用nio-API,您可以用一行代码读取整个文件。
  • To extract the values of an entry line I used a Pattern to split the line into an array with each single word as a value. 为了提取输入行的值,我使用了Pattern将行split成一个数组,每个单词作为一个值。
  • Then it is easy get the relevant values of an entry using the appropriate array indexes. 然后,使用适当的数组索引很容易获得条目的相关值。
  • To write the csv file line by line (without additional libs) you can use a BufferedWriter . 要逐行写csv文件(没有其他库),可以使用BufferedWriter
  • The file you're writting to is a resource. 您要写入的文件是一种资源。 It is recommended to use resources with the try-with-resource-statement . 建议将资源与try-with-resource-statement一起使用

I hope I could answer your question. 希望我能回答你的问题。

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

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