import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
/**
* 2015年12月5日下午4:25:54
*
* @author hp TODO 随机读取文件内容
*
*/
public class ReadLine {
List<String> list = new ArrayList<String>();
/**
* 获取随机行数
*
* @param total
* 文件总行数
* @return 整形参数
*/
public int getRandomNumber(int total) {
return (int) (Math.random() * total);
}
/**
* 将文件内容按行读取存放到List里面
*
* @param fileName
* 文件名
*/
public void initList(String fileName) {
try {
RandomAccessFile accessFile = new RandomAccessFile(fileName, "r");
String str = "";
while (null != (str = accessFile.readLine())) {
list.add(str);
}
accessFile.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 获取随机行数的字符串
*
* @return
*/
public String getStringOfFile() {
if (null != list) {
int line = getRandomNumber(list.size());
return list.get(line);
}
return null;
}
public static void main(String[] args) {
ReadLine rl = new ReadLine();
rl.initList("D://1.java");
System.out.println(rl.getStringOfFile());
}
}
追问ReadLine定义不了
追答错误是什么? 是不是你随便命名了Java文件?