turt2live

Number of lines counter

Feb 25th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Launcher {
  5.     static File dir = new File(System.getProperty("user.dir") + "\\Files\\");
  6.     static File log = new File(System.getProperty("user.dir")+"\\structure.txt");
  7.     static int lines = 0;
  8.     static int spc_count = -1;
  9.  
  10.     static void Process(File aFile) throws FileNotFoundException{
  11.         spc_count += 2;
  12.         String spcs = "";
  13.         for(int i = 0; i < spc_count; i++){
  14.             spcs += " ";
  15.         }
  16.         if(aFile.isFile()){
  17.             echo(spcs + "[FILE] " + aFile.getName());
  18.             countLines(aFile);
  19.         }else if(aFile.isDirectory()){
  20.             echo(spcs + "[DIR] " + aFile.getName());
  21.             File[] listOfFiles = aFile.listFiles();
  22.             if(listOfFiles != null){
  23.                 for(int i = 0; i < listOfFiles.length; i++){
  24.                     Process(listOfFiles[i]);
  25.                 }
  26.             }else{
  27.                 echo(spcs + " [ACCESS DENIED]");
  28.             }
  29.         }
  30.         spc_count -= 2;
  31.     }
  32.  
  33.     public static void main(String[] args) throws FileNotFoundException{
  34.         Process(dir);
  35.         echo("Total lines: " + lines);
  36.     }
  37.  
  38.     private static void countLines(File file) throws FileNotFoundException{
  39.         Scanner scanner = new Scanner(file);
  40.         while (scanner.hasNextLine()){
  41.             @SuppressWarnings ("unused")
  42.             String line = scanner.nextLine();
  43.             lines++;
  44.         }
  45.     }
  46.  
  47.     public static void echo(String str){
  48.         System.out.println(str);
  49.         addToLog(str);
  50.     }
  51.    
  52.     public static void addToLog(String loge){
  53.         try{
  54.             BufferedWriter out = new BufferedWriter(new FileWriter(log, true));
  55.             out.write(loge+"\r\n");
  56.             out.close();
  57.         }catch (Exception e){
  58.             e.printStackTrace();   
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment