Apache Camel 将文本文件解组为 Java 对象

问题描述 投票:0回答:1

 text data file, it shows ? mark there

Unexpected / unmapped characters found at the end of the fixed-length record at line : 1

 text file is having list of body items each length 1288, when giving single line of body record it is passing with length 1287.
java arraylist apache-camel unmarshalling bindy
1个回答
0
投票

读取错误,看起来文本文件包含 1287 个字符的记录,但程序期望包含 1288 个字符的记录。 要解决此问题,您可以更改程序中的记录长度并确保程序可以处理不同长度的记录 或使用类似的类在文本文件中每个记录的末尾添加填充字符:

class AddPadding {

    public static void addPadding(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        List<String> records = new ArrayList<>();
        String record;
        while ((record = reader.readLine()) != null) {
            records.add(record);
        }
        reader.close();

        for (int i = 0; i < records.size(); i++) {
            records.set(i, records.get(i) + " ");
        }

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (String r : records) {
            writer.write(r);
            writer.newLine();
        }
        writer.close();
    }

    public void addPaddingToMyFile() throws IOException {
        File file = new File("my-file.txt");
        AddPadding.addPadding(file);
    }
}

这将在文件 textdatafile.txt 中的每个记录的末尾添加一个填充字符。

© www.soinside.com 2019 - 2024. All rights reserved.