IO流中有个按行读的,你去看一下API,自己写个计数就可以了, public static void main(String[] args) { String str = ""; int n = 0; File ft = new File("E:\\temp\\comic.sql"); StringBuffer sb = new StringBuffer(); try { InputStream is = new FileInputStream(ft); BufferedReader in = new BufferedReader(new InputStreamReader(is, "GBK")); while (null != (str = in.readLine())) { n ++; } } catch (Exception e) { System.out.println("读数据错误"); } }
楼上的通过验证:)完整代码如下,需要在程序所在目录保存hello.txt文件(即想得到行数的文件) import java.io.*; class Test{ public static void main(String[] args) { String str = ""; int n = 0; String path = new File("").getAbsolutePath(); File ft = new File(path + "\\hello.txt"); StringBuffer sb = new StringBuffer(); try { InputStream is = new FileInputStream(ft); BufferedReader in = new BufferedReader(new InputStreamReader(is, "GBK")); while (null != (str = in.readLine())) { n ++; } } catch (Exception e) { System.out.println("读数据错误"); } System.out.println(n); } }
InputStream is=new FileInputStream("");
BufferedReader br=new BufferedReader(new InputStreamReader(is));String s;
while((s=br.readLine())!=null){
System.out.println(s);
}
String str = "";
int n = 0;
File ft = new File("E:\\temp\\comic.sql");
StringBuffer sb = new StringBuffer();
try {
InputStream is = new FileInputStream(ft);
BufferedReader in = new BufferedReader(new InputStreamReader(is,
"GBK"));
while (null != (str = in.readLine())) {
n ++;
}
} catch (Exception e) {
System.out.println("读数据错误");
}
}
import java.io.*;
class Test{
public static void main(String[] args) {
String str = "";
int n = 0;
String path = new File("").getAbsolutePath();
File ft = new File(path + "\\hello.txt");
StringBuffer sb = new StringBuffer();
try {
InputStream is = new FileInputStream(ft);
BufferedReader in = new BufferedReader(new InputStreamReader(is,
"GBK"));
while (null != (str = in.readLine())) {
n ++;
}
} catch (Exception e) {
System.out.println("读数据错误");
}
System.out.println(n);
}
}