java取前100个字节,Java扫描程序仅读取前2048个字节

I'm using java.util.Scanner to read file contents from classpath with this code:

String path1 = getClass().getResource("/myfile.html").getFile();

System.out.println(new File(path1).length()); // 22244 (correct)

String file1 = new Scanner(new File(path1)).useDelimiter("\\Z").next();

System.out.println(file1.length()); // 2048 (first 2k only)

Code runs from idea with command (maven test)

/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin/java -Dmaven.home=/usr/share/java/maven-3.0.4 -Dclassworlds.conf=/usr/share/java/maven-3.0.4/bin/m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 12 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/usr/share/java/maven-3.0.4/boot/plexus-classworlds-2.4.jar:/Applications/IntelliJ IDEA 12 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher --fail-fast --strict-checksums test

It was running perfectly on my win7 machine. But after I moved to mac same tests fail.

I tried to google but didn't find much =(

Why Scanner with delimiter \Z read my whole file into a string on win7 but won't do it on mac?

I know there're more ways to read a file, but I like this one-liner and want to understand why it's not working.

Thanks.

解决方案

Here is some info from java about it

\Z The end of the input but for the final terminator, if any

\z The end of the input

A line terminator is a one- or two-character sequence that marks the

end of a line of the input character sequence. The following are

recognized as line terminators:

A newline (line feed) character ('\n'), A carriage-return character

followed immediately by a newline character ("\r\n"), A standalone

carriage-return character ('\r'), A next-line character ('\u0085'), A

line-separator character ('\u2028'), or A paragraph-separator

character ('\u2029).

So use \z instead of \Z

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值