[英]New Line appearing when reading from input file, even though \n is Delimited
Hello I have this code: 你好我有这个代码:
Scanner input = new Scanner(new File("src/data/input.csv"));
input.useDelimiter(",|\\n");
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
System.out.println(input.next());
Input.csv is: Input.csv是:
-149.95038,61.13712,"McDonalds-Anchorage,AK","3828 W Dimond Blvd, Anchorage,AK, (907) 248-0597"
-149.95038,61.13712,“McDonalds-Anchorage,AK”,“3828 W Dimond Blvd,Anchorage,AK,(907)248-0597”
-149.93538,61.18167,"McDonalds-Anchorage,AK","4350 Spenard Rd, Anchorage,AK (907) 243-5122"
-149.93538,61.18167,“McDonalds-Anchorage,AK”,“4350 Spenard Rd,Anchorage,AK(907)243-5122”
My expected output is: 我的预期输出是:
-149.95038
-149.95038
61.13712
61.13712
"McDonalds-Anchorage
“麦当劳 - 安克雷奇
AK"
AK”
"3828 W Dimond Blvd
“3828 W Dimond Blvd
Anchorage
锚地
AK
AK
(907) 248-0597"
(907)248-0597“
-149.93538
-149.93538
61.18167
61.18167
Instead I get: 相反,我得到:
-149.95038
-149.95038
61.13712
61.13712
"McDonalds-Anchorage
“麦当劳 - 安克雷奇
AK"
AK”
"3828 W Dimond Blvd
“3828 W Dimond Blvd
Anchorage
锚地
AK
AK
(907) 248-0597"
(907)248-0597“
(blank line)
(空行)
-149.93538
-149.93538
61.18167
61.18167
I am not sure why the New Line isn't being skipped. 我不确定为什么不会跳过新线。 Is there a better way to get values from an input file?
有没有更好的方法从输入文件中获取值?
This is probably a Carriage Return/Line Feed issue . 这可能是回车/换行问题 。 On Windows (and a few other systems), the line delimiter is
\\r\\n
instead of \\n
. 在Windows(以及其他一些系统)上,行分隔符是
\\r\\n
而不是\\n
。
Replace 更换
input.useDelimiter(",|\\n");
with 同
input.useDelimiter(",|\\n|\\r|(\\r\\n)");
This will make sure that Carriage Return ( \\r
) is picked up. 这将确保拾取回车(
\\r
)。
However, the best way to read your file is to use a BufferedReader
instead of a Scanner
(you initialize it in the same way). 但是,读取文件的最佳方法是使用
BufferedReader
而不是Scanner
(以相同的方式初始化它)。 Then, use readLine()
to read your line and line.split(",")
to split your line at commas. 然后,使用
readLine()
读取你的行和line.split(",")
以逗号分割你的行。
尝试使用系统行分隔符
input.useDelimiter(System.getProperty("line.separator"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.