Monday, October 31, 2011

Getting number of lines in a file

The following code 'll return the number of lines in the file 'example.txt'
  1. public static int getNoOfLines(String filename){
  2.   File file = new File("example.txt");
  3.   if (!file.exists()){
  4.    System.out.println("File doesn't exist");
  5.    System.exit(0);
  6.   }
  7.   if (file.isDirectory()){
  8.    System.out.println("Expected file name but entered directory name");
  9.    System.exit(0);
  10.   }
  11.   int line = 0;
  12.   try {
  13.     LineNumberReader ln = new LineNumberReader(new FileReader(filename));                                                                        
  14.           while (ln.readLine() !null) {
  15.            line++;
  16.           }
  17.       } catch (IOException e) {
  18.        e.printStackTrace();
  19.       }
  20.   return line;  
  21.  }

Checking a file/directory present or not

The following code 'll return boolean values,true if the file/directory is present,false if the file/directory is not present.
  1. public boolean isFileOrDirectoryPresent(String fileOrDirName){
  2.   File file = new File(fileOrDirName);
  3.   if (!file.exists()){
  4.     System.out.println("File/Directory doesn't exist");
  5.     return false;
  6.   }
  7.   else {
  8.     System.out.println("File/Directory exists");
  9.     return true;
  10.   }
  11. }

Getting list of files/subdirectories in a directory

The following code 'll return the list of files that are present in the directory.This function accepts a parameter 'dirName',the directory in which you want the list of files.
  1. public String[] filesInDir(String dirName){
  2.   String[] files = null;  
  3.   try {
  4.     File dir = new File(dirName);
  5.     if (dir.isDirectory()) {
  6.       files = dir.list();
  7.     }
  8.     else if (!dir.exists()){
  9.       System.out.println("Directory doesn't exist");
  10.       System.exit(0);
  11.    }
  12.    } catch (Exception e) {
  13.      e.printStackTrace();
  14.    }  
  15.   return files;
  16. }

Copy directory in Java

The following code 'll copy the contents of a given source folder to a given destination folder.This method accepts 2 parameters,one for source folder location and another for destination folder location.
  1. public static void copyFolder(File srcFolder, File destFolder)  
    throws IOException{
  2.  
  3.  if(!srcFolder.exists()){
  4.     System.out.println("Source folder does not exist!");
  5.     System.exit(0);
  6.  }
  7.      
  8. //If folder/directory
  9.  if(srcFolder.isDirectory()){  
  10.  
  11.   //If destination directory doesn't exist, create it
  12.   if(!destFolder.exists()){
  13.      destFolder.mkdir();
  14.   }
  15.  
  16.   //List all the contents of source directory
  17.   String files[] = srcFolder.list();
  18.   for (String file : files) {
  19.      //construct the srcFolder and destFolder file structure
  20.      File srcFile = new File(srcFolder, file);
  21.      File destFile = new File(destFolder, file);
  22.  
  23.      //recursive copy
  24.      copyFolder(srcFile,destFile);
  25.   }
  26.  }
  27.  
  28.  //If file, then copy it
  29.  else{
  30.  
  31.  //Use bytes stream to support all file types
  32.   InputStream in = new FileInputStream(srcFolder);
  33.   OutputStream out = new FileOutputStream(destFolder);
  34.      byte[] buffer = new byte[1024];
  35.      int length;
  36.      
  37.      //copy the file content in bytes
  38.      while ((length = in.read(buffer)) > 0){
  39.      out.write(buffer, 0, length);
  40.      }
  41.      in.close();
  42.      out.close();
  43.  }
  44. }