Java:如何逐行读取文件,包括不同的行分隔符
Java : How to read a File line by line including different line separator
我们正在使用 JAVA 8 和
这是我的问题的上下文:
我们的程序中有这样一张地图:
<Key, object containing (record-offset, record-lentgh)
我们必须计算文件中每条记录的长度,应包括行分隔符 以计算每条记录的记录偏移量。
例如:
record-offset of 1st record in the file = 0
record-offset of 2nd record in the file =
record-offset of 1st record in the file
+ record length of 1st record
and so on...
在后面的过程中,我们将使用这些记录偏移量和记录长度信息通过 RandomAccessFile.
从文件中读取每条记录
这个过程很快,为我们节省了 运行 时间的内存。
现在的问题是:
当我使用 BefferedReader.readLine() 读取文件中的每条记录并根据返回字符串的长度计算记录长度和记录偏移时,此记录偏移计算被搞乱了。 BefferedReader 去除行分隔符。 DOS 文件的行分隔符是 \r\n,Unix/MAC 文件的行分隔符是 \n。因此,我后面使用 RandomAccessFile 读取文件的过程由于错误的偏移量而被搞砸了。看起来要修复我必须计算偏移量的问题,从第二条记录开始这样:
line-separator-length = 2;\for DOS or 1 for UNix type file
record-offset of 2nd record in the file =
record-offset of 1st record in the file
+ record length of 1st record
+ line-separator-length
因此,我的问题是:
- 有没有办法从包含行分隔符的文件中读取每一行(这样我就不必担心文件的类型)?
或
- 有什么方法可以从 JAVA 中找出它是什么类型的文件? (DOS/UNIX/MAC)
或
- 有什么方法可以检查文件中的行分隔符是什么?
提前致谢。
Is there anyway to read each line from a file that includes line-separator characters?
当然可以。使用 BufferedReader 作为模型扩展抽象 class Reader。包括行分隔符。
Is there any way to figure out what kind of file it is from?
当然可以。 Unix以换行(\n)结束,Windows以回车结束return,换行(\r\n),Mac(OS 10+ ) 以换行 (\n).
结尾
较早的 Macs 以回车符 return (\r) 结尾。
Is there any way I can check what are the line separator characters in a file?
您的 Reader class 将 return 字符串最后或最后 2 个位置的行分隔符。
这就是我解决问题的方法:感谢在以下方面的讨论:
How to find out which line separator BufferedReader#readLine() used to split the line?
public int getLineTerminatorLength( String filePath ) throws FileUtilitiesException
{
try (BufferedReader tempreader = FileUtilities.getBufferedReader( new File( filePath ) ))
{
String l = "";
char termChar = ' ';
while ( ( termChar = (char) tempreader.read() ) != -1 )
{
if ( ( termChar == '\n' ) || ( termChar == '\r' ) )
{
char ctwo = ' ';
if ( ( ctwo = (char) tempreader.read() ) != -1 )
{
if ( ( ctwo == '\n' ) || ( ctwo == '\r' ) )
return 2;
}
return 1;
}
}
}
catch ( Exception e )
{
String errMsg = "Error reading file " + filePath;
throw new FileUtilitiesException( errMsg );
}
//Will reach here if it is empty file
return 0;
}
我们正在使用 JAVA 8 和
这是我的问题的上下文:
我们的程序中有这样一张地图:
<Key, object containing (record-offset, record-lentgh)
我们必须计算文件中每条记录的长度,应包括行分隔符 以计算每条记录的记录偏移量。 例如:
record-offset of 1st record in the file = 0
record-offset of 2nd record in the file =
record-offset of 1st record in the file
+ record length of 1st record
and so on...
在后面的过程中,我们将使用这些记录偏移量和记录长度信息通过 RandomAccessFile.
从文件中读取每条记录这个过程很快,为我们节省了 运行 时间的内存。
现在的问题是:
当我使用 BefferedReader.readLine() 读取文件中的每条记录并根据返回字符串的长度计算记录长度和记录偏移时,此记录偏移计算被搞乱了。 BefferedReader 去除行分隔符。 DOS 文件的行分隔符是 \r\n,Unix/MAC 文件的行分隔符是 \n。因此,我后面使用 RandomAccessFile 读取文件的过程由于错误的偏移量而被搞砸了。看起来要修复我必须计算偏移量的问题,从第二条记录开始这样:
line-separator-length = 2;\for DOS or 1 for UNix type file
record-offset of 2nd record in the file =
record-offset of 1st record in the file
+ record length of 1st record
+ line-separator-length
因此,我的问题是:
- 有没有办法从包含行分隔符的文件中读取每一行(这样我就不必担心文件的类型)?
或
- 有什么方法可以从 JAVA 中找出它是什么类型的文件? (DOS/UNIX/MAC)
或
- 有什么方法可以检查文件中的行分隔符是什么?
提前致谢。
Is there anyway to read each line from a file that includes line-separator characters?
当然可以。使用 BufferedReader 作为模型扩展抽象 class Reader。包括行分隔符。
Is there any way to figure out what kind of file it is from?
当然可以。 Unix以换行(\n)结束,Windows以回车结束return,换行(\r\n),Mac(OS 10+ ) 以换行 (\n).
结尾较早的 Macs 以回车符 return (\r) 结尾。
Is there any way I can check what are the line separator characters in a file?
您的 Reader class 将 return 字符串最后或最后 2 个位置的行分隔符。
这就是我解决问题的方法:感谢在以下方面的讨论: How to find out which line separator BufferedReader#readLine() used to split the line?
public int getLineTerminatorLength( String filePath ) throws FileUtilitiesException
{
try (BufferedReader tempreader = FileUtilities.getBufferedReader( new File( filePath ) ))
{
String l = "";
char termChar = ' ';
while ( ( termChar = (char) tempreader.read() ) != -1 )
{
if ( ( termChar == '\n' ) || ( termChar == '\r' ) )
{
char ctwo = ' ';
if ( ( ctwo = (char) tempreader.read() ) != -1 )
{
if ( ( ctwo == '\n' ) || ( ctwo == '\r' ) )
return 2;
}
return 1;
}
}
}
catch ( Exception e )
{
String errMsg = "Error reading file " + filePath;
throw new FileUtilitiesException( errMsg );
}
//Will reach here if it is empty file
return 0;
}