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

xqk2d5yq  于 8个月前  发布在  Apache
关注(0)|答案(1)|浏览(77)

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.

字符串

fzwojiic

fzwojiic1#

阅读错误,看起来文本文件包含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("textdatafile.txt");
        AddPadding.addPadding(file);
    }
}

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

相关问题