import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.text.DecimalFormat; import java.text.NumberFormat; public class source { long num_of_lines = 0; long num_of_files = 0; String filetype; long num_of_filesize; public source( String path, String filetype ) { if( !filetype.startsWith(".") ){ filetype = "." + filetype; } this.filetype = filetype; displayFiles(path); NumberFormat nf = new DecimalFormat("# ###,###"); System.out.println("Filetype: " + filetype.substring(1) ); System.out.println("Files: " + nf.format( num_of_files ) ); System.out.println("Code lines: " + nf.format( num_of_lines ) ); System.out.println("Average: " + nf.format( num_of_lines/num_of_files) ); System.out.println("Files size: " + nf.format( ( num_of_filesize ) ) + "b" ); System.out.println(); } public void displayFiles( String path ){ //novy soubor File file = new File(path); //list souboru z otevreneho adresare String[] files = file.list(); //projdeme vsechny soubory for (int i = 0; files != null && i < files.length; i++) { //vytvorime novy File File f = new File(path + "\\" + files[i]); //pokud se jedna o adresar, rekurzivne volame if( f.isDirectory() ){ displayFiles(path + "\\" + files[i] ); } else{ //pokud soubor obsahuje spravnou priponu if( files[i].endsWith(filetype) ){ FileReader fr = null; try { //novy FileReader fr = new FileReader( f ); } catch (FileNotFoundException e) { e.printStackTrace(); } //zjistime pocet radek LineNumberReader ln = new LineNumberReader(fr); int count = 0; try { while (ln.readLine() != null) count++; } catch (IOException e) { e.printStackTrace(); } //pripocteme potrebne hodnoty num_of_lines+=count; num_of_files++; num_of_filesize += f.length(); } } } } public static void main(String[] args) { String path = new String("c:\\www\\www"); new source(path,"php"); path = new String("c:\\java\\www"); new source(path,"java"); path = new String("c:\\cproj"); new source(path,"cpp"); } }