Advertisement
Ravb

Balofao

Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. public class directoryMethods
  2. {
  3.    private String text;
  4.    String freq[];
  5.    private int keyWordCounter;
  6.    File[] subDirFileArray;
  7.    
  8.    
  9. public void caller(){printTree("c:",0);}
  10.  
  11. public void printTree(String path, int depth){
  12.     for(int i = 0 ; i < depth; i++){System.out.println("    ");}
  13.     System.out.println(path);
  14.     if(path.isDirectory()){
  15.         for(File sub:path.subFiles){
  16.             printTree(sub);
  17.         }
  18.     }
  19. }
  20.  
  21.    
  22.    
  23.    
  24.    public void DirectoryTree(){
  25.        String startOfDirectory = "c:/";
  26.        
  27.        File dirFile = new File(startOfDirectory);
  28.        File[] directory = dirFile.listFiles();
  29.        if(directory != null){
  30.        for(File x : directory){
  31.            System.out.println(startOfDirectory+ x.getName());
  32.            File subDirFile = new File(startOfDirectory+x.getName()+"/");
  33.            subDirFileArray = subDirFile.listFiles();
  34.            if(subDirFileArray != null){
  35.                //System.out.println("\t"+"Accessible");
  36.                /*for(File y : subDirFileArray){
  37.                System.out.println("\t"+y.getName());                      
  38.                }*/
  39.                for(File y : subDirFileArray){
  40.                    System.out.println("\t/"+y.getName());
  41.                    File sSubDirFile = new File (startOfDirectory+x.getName()+"/"+y.getName()+"/");
  42.                    File[] sSubDirFileArray = sSubDirFile.listFiles();
  43.                    if(sSubDirFileArray != null){
  44.                        for(File z : sSubDirFileArray){
  45.                            System.out.println("\t\t"+z.getName());
  46.                         }
  47.                    }else{System.out.println("\t\tNot accesible");}
  48.                }  
  49.                
  50.            }else{          
  51.                 System.out.println("\tNot accessible");
  52.            }          
  53.            }
  54.        }
  55.        else{
  56.            System.out.println("In C:/ Not accessible");
  57.        }
  58.    }
  59.  
  60.    public void AccesTest(){
  61.        File file = new File("c:/Program Files/");
  62.        File[] fileDir = file.listFiles();
  63.        if(fileDir != null){
  64.        for(File s : fileDir){
  65.            System.out.println(s.getName());
  66.        }
  67.        }
  68.        else
  69.        {
  70.            System.out.println("Acces Denied");
  71.        }    
  72.    }
  73.    
  74. /**Searches through all the text files in the given Folder for keywords.
  75.  *
  76.  */
  77.    
  78.    public void searchesAllTxtFilesInDirectoryForKeyWords(String whatKeyword) throws Exception
  79.    {
  80.        Scanner user_input = new Scanner(System.in);
  81.        System.out.println("Type directory name: ");
  82.        String user = user_input.next(); //User Input
  83.        
  84.        
  85.        //System.out.println(directory[0]);
  86.        String lKeyword = whatKeyword.toLowerCase();
  87.        File dirFile = new File(user);      
  88.        String[] directory = dirFile.list();
  89.        for(String s : directory){
  90.            if(s.contains(".txt")){
  91.                
  92.            FileReader file = new FileReader("C:/Test/"+s+"/");      
  93.            BufferedReader reader = new BufferedReader(file);
  94.        
  95.            String text = "";
  96.            String line = reader.readLine();
  97.            while(line != null)
  98.            {
  99.                text += "\n" + line ;
  100.                line = reader.readLine();
  101.            }
  102.        
  103.             String words[] = text.split("[,.!?():;\\s]+");
  104.          
  105.            for(int i=0;i<words.length;i++){
  106.                 String word = words[i].toLowerCase();
  107.                 if(word.equals(lKeyword)){              
  108.                     keyWordCounter += 1;
  109.                 }          
  110.            }
  111.            }
  112.            else
  113.            {
  114.                 System.out.println("Can only read .txt files");
  115.            }
  116.        
  117.    }
  118.    System.out.println("The name '"+whatKeyword+"' was mentioned "+keyWordCounter+" times");
  119. }
  120.    
  121.    public void getAllTextFilesInDir(){
  122.    
  123.    
  124.     }
  125.    
  126.    public void clearScreen()
  127.    {
  128.       System.out.print('\u000C');
  129.    }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement