我现在用的方法是:
fr=new FileReader(path+"\\"+xmlname+".xml");
br=new BufferedReader(fr);
Line=br.readLine();
int linNumber = 0;
for(;Line!=null;linNumber++){
Line=br.readLine();
} 这对于小文件来说还可以,但是如果文件比较大的话,效率就会很低,不知道还有没有其他更好的方法,还请各位指教,谢谢!!
fr=new FileReader(path+"\\"+xmlname+".xml");
br=new BufferedReader(fr);
Line=br.readLine();
int linNumber = 0;
for(;Line!=null;linNumber++){
Line=br.readLine();
} 这对于小文件来说还可以,但是如果文件比较大的话,效率就会很低,不知道还有没有其他更好的方法,还请各位指教,谢谢!!
如果是Windows,没有什么好的办法。
谢谢各位的指教!
虽然我还没有找到好一点的方法。 healer_kx(不会C的程序员那也算程序员?----甘草)
LineReader好象不是读行数的吧,愿闻其详。
我有一个小例子 你看看吧!
===========================================================================
import java.io.*;
public class file{
/*fileRead是将提供的一个参数filepath(文件路径)的文件内容读入页面
*@param filepath是String 类型
*@return fileRead返回filepath指定文件的内容
*/
public String fileRead(String filepath){
String content="";
try{
File f=new File(filepath);
BufferedReader br=new BufferedReader(new FileReader(f));
String temp=null;
int line=0;
while ((temp=br.readLine())!=null){
line++;
content+=temp+"\n";
}
System.out.println("一共有:"+line+"行");
}catch(FileNotFoundException e){
System.out.println("文件没有找到!");
}catch(IOException e){
System.out.println("IO读写错误!");
}
return content;
} public static void main(String[] args){
file f =new file();
System.out.println(f.fileRead("C:\\tomcat5\\webapps\\ROOT\\RELEASE-NOTES.txt"));
}
}
然后用indexof("\n")来统计回车符不是更好
FileReader fr = new FileReader(file);char[] buf = new char[1];
int read = 0;int count = 0;while((read=fr.read(buf))!=-1){
if(buf[0].equals("\n")) count++;
}想了一下 直接遍历字符不就行了
但绝不可以把文件全读出来,或者使用readLine,这两者都可能造成OutofMemory错误。