本文介绍了Java Scanner换行符识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到默认情况下指定扫描程序如何处理换行模式的文档。我想逐行读取文件,让扫描仪能够处理\ r,\ n或\\\\ n行结尾,无论程序实际运行的系统如何。



如果我声明这样的扫描仪:

 扫描仪扫描仪=新的扫描仪(读卡器); 

默认行为是什么?它会处理如上所述的所有三种类型,还是我必须明确告诉它呢?

解决方案

查看源代码Sun JDK 1.6的代码,使用的模式是\\\\ || [\\\\\\\\\\\\\\\\\\\\\\\\\\ >分别为行分隔符,段分隔符和下一行分别表示\\ n或\ r,\ n中的任何一个或unicode字符。


I can't find the documentation that specifies how a Scanner treats newline patterns by default. I want to read a file line by line and have the scanner be able to handle \r, \n or \r\n line endings regardless of the system the program is actually running on.

If I declare a scanner like so:

Scanner scanner = new Scanner(reader);

what is the default behaviour? Will it handle all three kinds as described above or do I have to tell it explicitly to do it?

解决方案

Looking at the source code for Sun JDK 1.6, the pattern used is "\r\n|[\n\r\u2028\u2029\u0085]"

which says "\r\n" or any one of \r, \n or the unicode characters for "line separator", "paragraph separator", and "next line" respectively.

这篇关于Java Scanner换行符识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:39