Parsing a fixed-width formatted file in Java

后端 未结 10 1968
遥遥无期
遥遥无期 2020-11-28 10:51

I\'ve got a file from a vendor that has 115 fixed-width fields per line. How can I parse that file into the 115 fields so I can use them in my code?

My first thought

相关标签:
10条回答
  • 2020-11-28 11:21

    Here is the plain java code to read fixedwidth file:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.List;
    
    public class FixedWidth {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            // String S1="NHJAMES TURNER M123-45-67890004224345";
            String FixedLengths = "2,15,15,1,11,10";
    
            List<String> items = Arrays.asList(FixedLengths.split("\\s*,\\s*"));
            File file = new File("src/sample.txt");
    
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                String line1;
                while ((line1 = br.readLine()) != null) {
                    // process the line.
    
                    int n = 0;
                    String line = "";
                    for (String i : items) {
                        // System.out.println("Before"+n);
                        if (i == items.get(items.size() - 1)) {
                            line = line + line1.substring(n, n + Integer.parseInt(i)).trim();
                        } else {
                            line = line + line1.substring(n, n + Integer.parseInt(i)).trim() + ",";
                        }
                        // System.out.println(
                        // S1.substring(n,n+Integer.parseInt(i)));
                        n = n + Integer.parseInt(i);
                        // System.out.println("After"+n);
                    }
                    System.out.println(line);
                }
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 11:27
    /*The method takes three parameters, fixed length record , length of record which will come from schema , say 10 columns and third parameter is delimiter*/
    public class Testing {
    
        public static void main(String as[]) throws InterruptedException {
    
            fixedLengthRecordProcessor("1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10", 10, ",");
    
        }
    
        public static void fixedLengthRecordProcessor(String input, int reclength, String dilimiter) {
            String[] values = input.split(dilimiter);
            String record = "";
            int recCounter = 0;
            for (Object O : values) {
    
                if (recCounter == reclength) {
                    System.out.println(record.substring(0, record.length() - 1));// process
                                                                                    // your
                                                                                    // record
                    record = "";
                    record = record + O.toString() + ",";
                    recCounter = 1;
                } else {
    
                    record = record + O.toString() + ",";
    
                    recCounter++;
    
                }
    
            }
            System.out.println(record.substring(0, record.length() - 1)); // process
                                                                            // your
                                                                            // record
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 11:29

    I've played arround with fixedformat4j and it is quite nice. Easy to configure converters and the like.

    0 讨论(0)
  • 2020-11-28 11:33

    I would use a flat file parser like flatworm instead of reinventing the wheel: it has a clean API, is simple to use, has decent error handling and a simple file format descriptor. Another option is jFFP but I prefer the first one.

    0 讨论(0)
提交回复
热议问题