JAVA读取TXT的问题
来源:百度知道 编辑:UC知道 时间:2024/06/18 11:18:16
2.怎么判断一个txt文件一共有多少行?
2.如果各位做上面这种程序,会用什么方法呢?是用List呢,还是用while循环判断呢.List我不太熟啊.....
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TextReader {
public static void main(String[] args) {
String url = "11.text"; //文件路径,我选择项目根目录了
String textArea = "相同的内容:"; //模仿JTextArea
String textField = "你好";
String line = "";
int count = 0; //计数器 记录有多少行
try {
FileReader fr = new FileReader(url);
BufferedReader br = new BufferedReader(fr);
line = br.readLine();
while(line !=null ){
System.out.println(line);
if(line.indexOf(textField)!= -1){
textArea += line;
}
line= br.readLine();
count++;
}
System.out.println(textArea);
System.out.println(count);
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在&quo