加急!! 如何定位读取文件中某行内容,文件写入时用的是FileInputStream
例如文件:
我要读取第6161行的信息如何做
[解决办法]
LineNumberReader lineBufferedReader = null;
try {
lineBufferedReader = new LineNumberReader(new InputStreamReader(new FileInputStream(
new File("E:\\javaTest\\中国心.txt")), "UTF-8"));// 这里可以根据情况选择指定字符集读取文件
String tmp = null;
// 跳过前三行
while (lineBufferedReader.getLineNumber() < 3) {
lineBufferedReader.readLine();
}
tmp = lineBufferedReader.readLine();
System.out.println(tmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != lineBufferedReader) {
lineBufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}