Friday, 31 October 2014

How to check if directory is empty in JAVA

Standard


In file class we have method called list() it Returns an array of strings naming the files and directories
 in the directory denoted by this abstract pathname.So by checking with length we can say directory
is empty or not. Let's go for code..













----------------------------------------------------------------------------------------------------------------------------------



JAVA CODE :




  1. package com.system;  
  2.   
  3. import java.io.File;  
  4.   
  5. public class Test  
  6. {  
  7.     public static void main(String[] args)  
  8.     {   
  9.    
  10.  File file = new File("D:\\newfolder");  
  11.         
  12.  long n=file.list().length;  
  13.         
  14.       System.out.println("Total files"+n);  
  15.  if(file.isDirectory()){  
  16.    
  17.   if(file.list().length>0){  
  18.    
  19.    System.out.println("Directory is not empty!!!!");  
  20.    
  21.   }else{  
  22.    
  23.    System.out.println("Directory is empty!!!!");  
  24.    
  25.   }  
  26.    
  27.  }else{  
  28.    
  29.   System.out.println("This is not a directory");  
  30.    
  31.  }  
  32.     }  
  33. }  

Thursday, 30 October 2014

How to get The total number of lines in a file by using JAVA

Standard


In this Post we will see how to Read Total Number of Lines in File Using JAVA Application

In java by using LineNumberReader class we can get  the total number of lines in a file by looping theLineNumberReader.readLine() method.Let's go for code.



----------------------------------------------------------------------------------------------------------------------------------








----------------------------------------------------------------------------------------------------------------------------------
JAVA CODE :

  1. package com.system;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.LineNumberReader;  
  7.   
  8. public class Test   
  9. {  
  10.     public static void main(String[] args)  
  11.     {     
  12.    
  13.         try{  
  14.    
  15.             File file =new File("D:\\file1234.txt");  
  16.    
  17.             if(file.exists()){  
  18.    
  19.                 FileReader fr = new FileReader(file);  
  20.                 LineNumberReader lnr = new LineNumberReader(fr);  
  21.                 
  22.                   
  23.                 int lineNum = 0;  
  24.    
  25.                     while (lnr.readLine() != null){  
  26.                           
  27.                         lineNum++;  
  28.                       
  29.                     }  
  30.    
  31.                     System.out.println("Total number of lines in the file : " + lineNum);  
  32.    
  33.                     lnr.close();  
  34.    
  35.    
  36.             }else{  
  37.                  System.out.println("File does not exists!");  
  38.             }  
  39.    
  40.         }catch(IOException e){  
  41.             e.printStackTrace();  
  42.         }  
  43.    
  44.     }  
  45. }