import java.io.File;
import java.io.RandomAccessFile;
/**
* 读取文档的第二行内容
*
* @author 3306 2017年3月21日
* @see
* @since 1.0
*/
public class ReadSecondLine {
/*
* 读取文件绝对路径
*/
private static String filePath = "d:/cms.sql";
public static void main(String[] args) {
String secodLine = readLine(filePath, 2);
System.out.println(secodLine);
}
/**
* 根据读取文件内容
*
* @param path
* 文件路径
* @param lineNum
* 行号
* @return String
*/
public static String readLine(String path, int lineNum) {
if (lineNum < 1) {
return "";
}
String line = "";
try {
RandomAccessFile file = new RandomAccessFile(new File(path), "r");
int index = 0;
while (null != (line = file.readLine())) {
if (++index == lineNum) {
break;
}
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
}