Apache Camel路由写入文件时换行符无法显示的问题
问题原因
你在simple表达式里通过代码换行写入的换行符不会被Camel识别为输出内容的一部分,要么是编译阶段被移除,要么是simple解析时被过滤,实际写入文件的内容只有asda字符串,没有换行符,所以最终输出是连续拼接的结果。你修改Notepad++的行尾显示规则没用,是因为文件本身就没有写入任何换行字符,不是显示格式的问题。
解决方案
方案1(推荐):使用file组件内置的自动追加换行参数
Apache Camel的file组件提供appendNewLine参数,设置为true后,每次执行追加操作时会自动在写入内容末尾加上系统对应的换行符,无需手动拼接换行,修改后代码如下:
.transform().simple("asda") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt") .transform().simple("asda") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt&fileExist=Append&appendNewLine=true") .transform().simple("asda") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt&fileExist=Append&appendNewLine=true")
方案2:手动在simple表达式里写入转义换行符
如果你需要自己控制换行符格式,不要在代码里直接敲回车生成换行,而是用转义字符显式声明:
// 使用\n作为换行符,需要Windows的CRLF换行就写\r\n .transform().simple("asda\n") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt") .transform().simple("asda\n") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt&fileExist=Append") .transform().simple("asda\n") .to("file:{{file.tempLocation}}?fileName=${header.csvFileName}.txt&fileExist=Append")
两种方案都可以实现每行内容单独换行的需求。
内容的提问来源于stack exchange,提问作者Sneharghya Pathak
相关产品推荐
相关产品推荐

