java读取大文件并处理
原地址:https://www.jb51.net/article/120760.htm
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, UTF-8);
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
}catch(IOException e){
logger.error(e);
}finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
LineIterator it = FileUtils.lineIterator(theFile, UTF-8);
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
LineIterator.closeQuietly(it);
}
还没有评论,来说两句吧...