Am 텍스트 파일을 읽고 여러 줄 "/ * * /"주석 및 한 줄 주석 "//"을 제외하고 줄 수를 계산하려고합니다. 텍스트 파일에서 "myMethod() {}" 의 모든 메서드를 찾고 계산하여 텍스트 파일에있는 메서드의 총 수를 계산합니다. 텍스트 파일 "java MyCountLine C : \ myFolder \ myText.txt"와 같은 java 프로그램을 실행하는 동안 텍스트 파일이 추가됩니다. 텍스트 파일에서 메서드를 가져 오는 방법을 알아낼 수 없습니다.자바에서 주석 및 새 라인을 제외하고 텍스트 파일의 라인 수를 계산하십시오.
import java.io.*;
public class LineCountss {
public static void main(String[] args) {
File inFile = null;
if (0 < args.length) {
// text file will be passed during run time
inFile = new File(args[0]);
} else {
System.out.println("Cant Find The File Specified : " + inFile);
}
BufferedReader br = null;
String sCurrentLine = null;
String func = null;
int a = 0, b = 0, c = 0, k = 0;
try {
// passing the text file location for FileReader.
br = new BufferedReader(new FileReader(inFile));
// Looping through the text file
while ((sCurrentLine = br.readLine()) != null) {
// avoid multi-line comments and one line comments and new
// lines.
if ((sCurrentLine.startsWith("/*") && sCurrentLine
.endsWith("*/"))
|| sCurrentLine.startsWith("//")
|| sCurrentLine.isEmpty()
|| (sCurrentLine.trim().matches("[{};]+"))) {
// count the number of comment lines and new lines to
// exclude it from count.
b++;
// Getting any function in the text file that start and end
// with().
} else if (sCurrentLine.contains("\\(\\)\\{")) {
func = sCurrentLine.trim();
// printing the functions/methods
System.out.println(func);
// counting the number of functions/ methods found
k++;
} else {
// printing the text file just for checking
System.out.println(sCurrentLine);
// count the total number of lines
a++;
}
}
// excluding the number of lines that has comments and new lines
c = a - b;
// printing the number of lines excluding comments and new lines
System.out.println("Number of Lines are : " + c);
// printing the number of lines of the functions/method found inside
// the text file.
System.out.println("Number of Functions are : " + k);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
try {
// close bufferReader
if (br != null) {
br.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
다음에 코드를 사용 블록,하지 인용. –
@Kuba Rakoczy Thanx 많은 동생들. 편집 해 주셔서 감사합니다. –