遍历文件列表以检查里面有多少行
iterating through a list of files to check how many lines are there inside
我一直在尝试编写一个简单的代码,我可以在其中遍历文件名列表,遍历每个文件名,并计算每个文件中有多少行。
但是下面的代码似乎根本不起作用(不断在 Eclipse 中启动调试透视图)。
public class fileScanner {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
//add files
list.add("C:\Users\HuiHui\Documents\eclipse\test.txt");
for (String l : list){
fileScan(l);
}
}
public static int fileScan(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean endsWithoutNewLine = false;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
endsWithoutNewLine = (c[readChars - 1] != '\n');
}
if(endsWithoutNewLine) {
++count;
}
return count;
} finally {
is.close();
}
}
}
任何人都可以阐明这一点吗?
您可以使用 Scanner 遍历每个文件,然后为存在的每一行递增一个整数。
示例:
while(scanner.hasNextLine()) {
scanner.nextLine();
integer++;
}
您使用 InputStream
并创建 BufferedInputStream
而不是使用 BufferedReader
的原因是什么?
为你的 filescan
方法试试这个
int filescan(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
int count=0;
String s;
while((s=br.nextLine()) != null)
count++;
br.close();
return count;
}
尝试 LineNumberReader
:
public static int fileScan(String filename) throws IOException {
File file = new File(filename);
LineNumberReader lnr = null;
try {
lnr = new LineNumberReader(new FileReader(file));
lnr.skip(file.length());//go to end of file
return lnr.getLineNumber();
} finally {
if(null != lnr) {
lnr.close();
}
}
}
我一直在尝试编写一个简单的代码,我可以在其中遍历文件名列表,遍历每个文件名,并计算每个文件中有多少行。 但是下面的代码似乎根本不起作用(不断在 Eclipse 中启动调试透视图)。
public class fileScanner {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
//add files
list.add("C:\Users\HuiHui\Documents\eclipse\test.txt");
for (String l : list){
fileScan(l);
}
}
public static int fileScan(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean endsWithoutNewLine = false;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
endsWithoutNewLine = (c[readChars - 1] != '\n');
}
if(endsWithoutNewLine) {
++count;
}
return count;
} finally {
is.close();
}
}
}
任何人都可以阐明这一点吗?
您可以使用 Scanner 遍历每个文件,然后为存在的每一行递增一个整数。
示例:
while(scanner.hasNextLine()) {
scanner.nextLine();
integer++;
}
您使用 InputStream
并创建 BufferedInputStream
而不是使用 BufferedReader
的原因是什么?
为你的 filescan
方法试试这个
int filescan(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
int count=0;
String s;
while((s=br.nextLine()) != null)
count++;
br.close();
return count;
}
尝试 LineNumberReader
:
public static int fileScan(String filename) throws IOException {
File file = new File(filename);
LineNumberReader lnr = null;
try {
lnr = new LineNumberReader(new FileReader(file));
lnr.skip(file.length());//go to end of file
return lnr.getLineNumber();
} finally {
if(null != lnr) {
lnr.close();
}
}
}