- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class CalCodeLine {
- /**
- * @author silencer
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- String filePath ="";
- File file = new File(filePath);
- System.out.println(" all code line :"+getFileLine(file));
- }
- /**
- *
- * @param file
- * @return fileLine
- * @throws IOException
- */
- public static int getFileLine(File file) throws IOException {
- int sFile = 0;
- if (file.isDirectory()) {
- File[] files = file.listFiles();
- for (File tempFile : files) {
- if (tempFile.isDirectory()) {
- sFile += getFileLine(tempFile);
- } else {
- sFile += getJavaTextLength(tempFile);
- }
- }
- }else{
- sFile += getJavaTextLength(file);
- }
- return sFile;
- }
- /**
- *
- * @param file
- * @return textLine
- * @throws IOException
- */
- public static int getJavaTextLength(File file) throws IOException {
- int sLength = 0;
- if (file != null && file.getName().endsWith(".java")) {
- BufferedReader br = new BufferedReader(new FileReader(file));
- StringBuffer sb = new StringBuffer();
- sb.append(br.readLine());
- boolean isComment = false;
- boolean isSigleComment = false;
- while (!sb.toString().equals("null")) {
- if (sb.toString().startsWith("//")) {
- isSigleComment = true;
- } else if (sb.toString().contains("/*")) {
- isComment = true;
- } else if (sb.toString().contains("*/")) {
- isComment = false;
- sLength -= 1;
- }
- if (!isSigleComment && !isComment
- && !sb.toString().startsWith("import")
- && !sb.toString().isEmpty()) {
- sLength += 1;
- }
- isSigleComment = false;
- sb = sb.delete(0, sb.length());
- sb.append(br.readLine());
- }
- br.close();
- }
- return sLength;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/280820135371.html
来源: http://www.codesnippet.cn/detail/280820135371.html