java中的LineCount程序。忽略计数中的某些行
我有一个Java程序,代码如下:
import java.io.*;
public class LineCountingProg
{
public static void main(String args[])
{
FileInputStream fis = null;
try
{
fis = new FileInputStream("d://MyFile.txt");
}
catch(FileNotFoundException e)
{
System.out.println("The source file does not exist. " +e );
}
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(fis));
String nextLine = null;
try {
while ((nextLine = lineCounter.readLine()) != null) {
System.out.println(nextLine);
if ((nextLine.startsWith("/*") && nextLine.endsWith("*/"))
|| (nextLine.trim().matches("[{};]+"))) {
//This line needs to be ignored
ignoredLines++;
continue;
}
}
System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
int fcounter = 0;
fcounter = lineCounter.getLineNumber()-ignoredLines ;
System.out.println("Total " +fcounter);
} catch (Exception done) {
done.printStackTrace();
}}}
我想添加一段代码,这样如果一行以/*开头,以*/结尾,它就可以忽略行计数。如果一行只包含;而且}也应该忽略这些行。我该怎么做
# 1 楼答案
在while循环之外,首先需要创建一个变量来计算所有被忽略的行
在while循环中,可以添加以下条件:
最后:
在
String
类中可以找到startsWith
、endsWith
和equals