Skip to content

Instantly share code, notes, and snippets.

@MhmdRyhn
Created June 28, 2019 15:36
Show Gist options
  • Save MhmdRyhn/41396f4924c90c21b7659baed827ddf5 to your computer and use it in GitHub Desktop.
Save MhmdRyhn/41396f4924c90c21b7659baed827ddf5 to your computer and use it in GitHub Desktop.
package total.nooflines;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TotalNoOfLines {
private long cnt = 0;
public long totalLine(String project) throws FileNotFoundException, IOException {
File dir = new File(project);
if (!dir.exists() || !dir.isDirectory()) {
System.out.println("Invalid Path");
return 0;
} else {
File[] files = dir.listFiles();
// for (int i = 0; i < files.length; i++)
for (File file : files) {
String newPath = project + "\\" + file.getName();
if (file.isFile()) {
//System.out.println("File Name: " + file.getName());
cnt += lineInFile(newPath);
} else {
//System.out.println("Directory Name: " + file.getName());
totalLine(newPath);
}
}
}
return cnt;
}
private long lineInFile(String path) throws FileNotFoundException, IOException {
long line = 1;
File f = new File(path);
BufferedReader br = new BufferedReader(new FileReader(f));
String readLine;
while ((readLine = br.readLine()) != null) {
line++;
}
return line;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
TotalNoOfLines totalLine = new TotalNoOfLines();
String projectPath = "E:\\\\MyCodes\\\\Rough\\\\Jobtest";
System.out.println(totalLine.totalLine(projectPath));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment