使用\ Z vs \ z作为扫描仪分隔符的行为 [英] Behavior of using \Z vs \z as Scanner delimiter

查看:157
本文介绍了使用\ Z vs \ z作为扫描仪分隔符的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了答案,但由于新用户的限制,我无法回答这个问题。无论哪种方式,这都是Java中的已知错误。



http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8028387



我正在尝试在64位ubuntu上将文件读入Java 6中的字符串。 Java给了我一个非常奇怪的结果,用\\Z它读取整个文件,但是用\\z 它会读取最多1024个字符的整个字符串。我已经阅读了所有类的Java 6 API,但我很茫然。



\ Z和\z的说明可以在以下位置找到:



http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#lt



<什么可能导致这种奇怪的行为?

  String fileString = new Scanner(new File(fileName))。useDelimiter(\\\\)。next( ); 
String fileString2 = new Scanner(new File(fileName))。useDelimiter(\\Z)。next();
System.out.println(使用Z:+ fileString2.length());
System.out.println(Using z+ fileString.length());

输出:
使用Z:9720
使用z:1024



谢谢!



有关文件/ java版本的详细信息:



使用java-6-openjdk-amd64运行Ubuntu(也使用oracle java6测试)
文件是简单的文本文件UTF-8编码。

解决方案

模式文档状态




  • \ z 输入结束

  • \ Z 输入结束但最终终止符(如果有)



我怀疑是因为扫描仪缓冲区大小设置为 1024

  354  私人 静态 最终  int  BUFFER_SIZE = 1024; //更改为1024; 

扫描程序读取此数量的字符并将其用作当前输入,因此 \ z 可以在这里用来表示它的结束,而 \Z 不能,因为它不是最终终结者(还有更多的元素)在整个输入中阅读)。


[Edit] I found the answer, but I can't answer the question due to restrictions on new users. Either way, this is a known bug in Java.

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8028387

I'm trying to read a file into a string in Java 6 on 64 bit ubuntu. Java is giving me the very strange result that with "\\Z" it reads the entire file, but with "\\z" it reads the entire string up to 1024 characters. I've read the Java 6 API for all the classes and I am at a loss.

Description of \Z and \z can be found at:

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#lt

What could be causing this strange behavior?

String fileString = new Scanner(new File(fileName)).useDelimiter("\\z").next();
String fileString2 = new Scanner(new File(fileName)).useDelimiter("\\Z").next();
System.out.println("using Z : " + fileString2.length());
System.out.println("Using z "+ fileString.length());

Output: using Z : 9720 Using z : 1024

Thanks!

Details about the file/java-version:

Running Ubuntu with java-6-openjdk-amd64 (tested also with oracle java6) File is simple text file UTF-8 encoded.

解决方案

As Pattern documentation states

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

I suspect that since Scanners buffer size is set to 1024,

354  private static final int BUFFER_SIZE = 1024; // change to 1024;

Scanner reads this amount of characters and uses it as current input, so \z can be used here to represent its end, while \Z can't because it is not "final terminator" (there are more elements in entire input to read).

这篇关于使用\ Z vs \ z作为扫描仪分隔符的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆