Java 如何找出用于拆分行的行分隔符BufferedReader#readLine()?
我正在通过BufferedReader读取文件Java 如何找出用于拆分行的行分隔符BufferedReader#readLine()?,java,bufferedreader,java-io,linefeed,Java,Bufferedreader,Java Io,Linefeed,我正在通过BufferedReader读取文件 String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String s = br.readLine(); if (s == null) break; ... } 我需要知道这些行是用“\n”还是“\r\n”分隔的 有没有办法让我知道 我不想打开FileInputStream以便开始扫描它。 理想情
String filename = ...
br = new BufferedReader( new FileInputStream(filename));
while (true) {
String s = br.readLine();
if (s == null) break;
...
}
我需要知道这些行是用“\n”还是“\r\n”分隔的
有没有办法让我知道
我不想打开FileInputStream以便开始扫描它。
理想情况下,我想询问BufferedReader,因为它必须知道
我很乐意覆盖BufferedReader来破解它,但我真的不想打开文件流两次
谢谢
注意:当前行分隔符(由System.getProperty(“line.separator”)返回)无法使用,因为文件可能已由另一个操作系统上的另一个应用程序写入。
BufferedReader
不接受FileInputStreams
否,无法找到BufferedReader正在读取的文件中使用的行终止符字符。该信息在读取文件时丢失
不幸的是,下面所有的答案都是错误的
编辑:是的,您可以随时扩展BufferedReader以包含所需的附加功能。
BufferedReader.readLine()
不提供任何方法来确定换行符是什么。若你们需要知道,你们需要自己阅读文字,并自己发现换行符
您可能对来自的内部类(以及它所使用的公共类)感兴趣
LineBuffer
提供回调方法void handleLine(字符串行,字符串结束)
其中end
是换行字符。你也许可以在这个基础上做一些你想做的事情。API可能看起来像public Line readLine()
,其中Line
是一个同时包含行文本和行尾的对象。如果您碰巧正在将此文件读入Swing text组件,则可以使用JTextComponent.read(…)方法将文件加载到文档中。然后您可以使用:
textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty );
要获取文件中使用的实际EOL字符串。在读取后(我承认是pythonista),似乎没有一种干净的方法来确定特定文件中使用的行结束编码
我建议您最好使用BufferedReader.read()
并迭代文件中的每个字符。大概是这样的:
String filename = ...
br = new BufferedReader( new FileInputStream(filename));
while (true) {
String l = "";
Char c = " ";
while (true){
c = br.read();
if not c == "\n"{
// do stuff, not sure what you want with the endl encoding
// break to return endl-free line
}
if not c == "\r"{
// do stuff, not sure what you want with the endl encoding
// break to return endl-free line
Char ctwo = ' '
ctwo = br.read();
if ctwo == "\n"{
// do extra stuff since you know that you've got a \r\n
}
}
else{
l = l + c;
}
if (l == null) break;
...
l = "";
}
答案是你无法找出这句话的结尾
我正在寻找什么可以导致相同函数中的行结束。查看BufferedReader源代码后,我可以确定BufferedReader.readLine在'\r'或'\n'上结束,并跳过leftower'\r'或'\n'。硬编码,不关心设置。要与BufferedReader类同步,可以使用以下方法处理\n\r\n\r和\r\n端点分隔符:
public static String retrieveLineSeparator(File file) throws IOException {
char current;
String lineSeparator = "";
FileInputStream fis = new FileInputStream(file);
try {
while (fis.available() > 0) {
current = (char) fis.read();
if ((current == '\n') || (current == '\r')) {
lineSeparator += current;
if (fis.available() > 0) {
char next = (char) fis.read();
if ((next != current)
&& ((next == '\r') || (next == '\n'))) {
lineSeparator += next;
}
}
return lineSeparator;
}
}
} finally {
if (fis!=null) {
fis.close();
}
}
return null;
}
如果您正在使用groovy,只需执行以下操作:
def lineSeparator = new File('path/to/file').text.contains('\r\n') ? '\r\n' : '\n'
不确定是否有用,但有时我需要在阅读文件后找出行分隔符 在这种情况下,我使用以下代码:
/**
* <h1> Identify which line delimiter is used in a string </h1>
*
* This is useful when processing files that were created on different operating systems.
*
* @param str - the string with the mystery line delimiter.
* @return the line delimiter for windows, {@code \r\n}, <br>
* unix/linux {@code \n} or legacy mac {@code \r} <br>
* if none can be identified, it falls back to unix {@code \n}
*/
public static String identifyLineDelimiter(String str) {
if (str.matches("(?s).*(\\r\\n).*")) { //Windows //$NON-NLS-1$
return "\r\n"; //$NON-NLS-1$
} else if (str.matches("(?s).*(\\n).*")) { //Unix/Linux //$NON-NLS-1$
return "\n"; //$NON-NLS-1$
} else if (str.matches("(?s).*(\\r).*")) { //Legacy mac os 9. Newer OS X use \n //$NON-NLS-1$
return "\r"; //$NON-NLS-1$
} else {
return "\n"; //fallback onto '\n' if nothing matches. //$NON-NLS-1$
}
}
/**
*标识字符串中使用的行分隔符
*
*这在处理在不同操作系统上创建的文件时非常有用。
*
*@param str-带神秘线分隔符的字符串。
*@返回windows的行分隔符,{@code\r\n},
*unix/linux{@code\n}或旧版mac{@code\r}
*如果无法识别,则返回到unix{@code\n}
*/
公共静态字符串identifyLineDelimiter(字符串str){
如果(str.matches(“(?s)。*(\\r\\n)。*”){//Windows/$NON-NLS-1$
返回“\r\n”/$NON-NLS-1$
}else如果(str.matches(“(?s)。*(\\n)。*”){//Unix/Linux/$NON-NLS-1$
返回“\n”/$NON-NLS-1$
}else if(str.matches(“(?s)。*(\\r)。*”){//Legacy mac os 9。较新的os X使用\n//$NON-NLS-1$
返回“\r”/$NON-NLS-1$
}否则{
返回“\n”;//如果没有匹配项,则返回到“\n”。//$NON-NLS-1$
}
}
也许你可以用扫描仪来代替
您可以将正则表达式传递给Scanner#useDelimiter()
以设置自定义分隔符
String regex=“(\r)?\n”;
字符串文件名=。。。。;
扫描仪扫描=新扫描仪(新文件输入流(文件名));
scan.useDelimiter(Pattern.compile(regex));
while(scan.hasNext()){
String str=scan.next();
//待办事项
}
您可以使用下面的代码将BufferedReader
转换为Scanner
新扫描仪(bufferedReader);
@gshauger:你可以说很多问题都是这样,但这并不意味着使用一个就更好了。在LineBuffer
的情况下,它是内部的,因此添加整个库不会有帮助。。。他可以直接复制那个文件。我不会说很多问题…只有那些不需要不必要的依赖性的问题…这是你推荐的。另外,这不是你第一次不必要地对番石榴库进行鞭笞。@ GSHOGGER:当别人编写的代码会让你免于不得不自己编写时,有时候使用它是很有用的,尤其是当你认为这样的小问题很少孤立存在时。我碰巧对番石榴非常熟悉,所以我倾向于建议使用它的解决方案,因为我认为它们比只使用JDK做额外的工作更容易或更合适。你对图书馆的明显厌恶并不影响我答案的正确性。(我的主要意思是OP可能想引用一些现有的代码来做他想做的事情。)@gshauger:我不喜欢编写和维护大量其他人已经编写和测试过的代码,这些代码将为您维护,以及这些代码对用户的影响“一个经过适当设计的软件的质量、可扩展性、可部署性和可用性”。我同意应该仔细选择依赖项,但我个人认为Guava具有极高的功率重量比,而且大多数Java