我正在通过BufferedReader读取文件
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”)返回),因为该文件可能已由另一个应用程序在另一个操作系统上写入。
阅读了java docs(我承认是一名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类保持一致,可以使用以下方法来处理\ 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;
}
BufferedReader.readLine()
不提供任何确定换行符的方法。如果您需要知道,则需要阅读自己的字符并自己找到换行符。
您可能对LineBuffer中的内部Guava类(及其使用的公共LineReader类)感兴趣。 LineBuffer
提供了一种回调方法void handleLine(String line, String end)
,其中end
是换行符。您可能可以基于此来做您想做的事情。 API可能类似于public Line readLine()
,其中Line
是包含行文本和行尾的对象。
[BufferedReader
不接受FileInputStreams
否,您无法找到BufferedReader读取的文件中使用的行终止符。读取文件时该信息会丢失。
很遗憾,以下所有答案都不正确。
编辑:是的,您始终可以扩展BufferedReader以包括所需的其他功能。
答案是您找不到行的结尾。
我正在寻找可导致同一功能中的行尾出现的原因。看完BufferedReader源代码之后,我可以说BufferedReader.readLine在'\ r'或'\ n'处结束并跳过左行'\ r'或'\ n'。硬编码,不关心设置。
如果碰巧将此文件读取到Swing文本组件中,则可以使用JTextComponent.read(...)方法将文件加载到Document中。然后您可以使用:
textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty );
获取文件中使用的实际EOL字符串。
也许您可以改用Scanner
。
您可以将正则表达式传递给Scanner#useDelimiter()
以设置自定义分隔符。
String regex="(\r)?\n";
String filename=....;
Scanner scan = new Scanner(new FileInputStream(filename));
scan.useDelimiter(Pattern.compile(regex));
while (scan.hasNext()) {
String str= scan.next();
// todo
}
您可以在下面使用此代码将BufferedReader
转换为Scanner
new Scanner(bufferedReader);
不确定是否有用,但是有时候我已经在很远的地方读取了文件之后需要找出行定界符。
在这种情况下,我使用此代码:
/**
* <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$
}
}
如果您使用的是groovy,则只需执行以下操作:
def lineSeparator = new File('path/to/file').text.contains('\r\n') ? '\r\n' : '\n'