java 获取换行符,读取带有换行符的Java文件

I have a Unicode file that needs to be exported to database(Vertica). The column delimiter is CTRL+B, record delimiter is newline(\n). Whenever there is a newline within a column value, CTRL+A is used as escape character.

When I use BufferedReader.readLine() to read this file, the records with ID's 2 and 4, are read as two records. Whereas I want to read them as a single whole record as given in output.

Here is the example input file. | stands for CTRL+B and ^ stands for CTRL+A.

Input

ID|Name|Job Desc

----------------

1|xxxx|SO Job

2|YYYY|SO Careers^

Job

3|RRRRR|SO

4|ZZZZ^

ZZ|SO Job

5|AAAA|YU

Output:

ID|Name|Job Desc

----------------

1|xxxx|SO Job

2|YYYY|SO Careers Job

3|RRRRR|SO

4|ZZZZ ZZ|SO Job

5|AAAA|YU

The file is huge, so I cant use StringEscapeUtils. Any suggestions on this?

解决方案

You can use a Scanner with a custom delimeter. The delimeter I use is set to match \n but not \u0001\n (where \u0001 represents CTRL+A):

try {

PrintWriter writer = new PrintWriter("dboutput.txt");

Scanner sc = new Scanner(new File("dbinput.txt"));

sc.useDelimiter(Pattern.compile("^(?!.*(\\u0001\\n)).*\\n$"));

while (sc.hasNext()) {

writer.println(sc.next());

}

scanner.close();

writer.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值