skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Showing posts with label Java File Example. Show all posts
Showing posts with label Java File Example. Show all posts
Saturday, October 27, 2012

Excel Data To Delimited File Utility

Posted by Raju Gupta at 7:30 PM – 0 comments
 

A windows based File converter utility has been developed (with Java) to convert an excel file into a delimited format.

This utility takes two inputs ,need to be provided by the user :
1) The path of the excel file (.xls/.xlt) required to be converted
2) Delimiter to be used (For example : pipe (|), comma (,), dollar ($) )

A delimited file named output_<worksheet>.txt is created ,on the same location as of input excel file. Also ,the utility is able to read multiple worksheets of the excel and the output file(s) should be opened with notepad.   

/*GetUserInput.java*/


import java.io.*;

public class GetUserInput
{  
 /*Method to take excel file path and name input from user*/
 String input()
 {
  System.out.print("Enter excel file name in the pattern C:\FolderName\filename " + "n");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String inputfile = null;
  try
  {
   inputfile = br.readLine();
  }catch (IOException e) 
   {         
   System.out.println("Error in input file!");        
   System.exit(1);      
   }
  return inputfile;
 }/*Method input closed*/
 
 /*Method to take delimiter to be used from user*/
 String delimiter()
 {
  System.out.print("Enter delimiter to be used " + "n");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String delimiter = null;
  try
  {
   delimiter = br.readLine();
  }catch (IOException e) 
  {         
   System.out.println("Error in delimiter!");        
   System.exit(1);      
  }
  return delimiter;
 }/*Method delimiter closed*/
}/*Class closed*/

/****************************************************************************************/
/*ConvertExcelToCSV.java*/

import java.io.*;
import jxl.*;
import java.util.*;
class  ConvertExcelToDelimited
{
 public static void main(String[] args)
 {
  try
  {
   /*To Get the input excel name from user*/
   GetUserInput input1 = new GetUserInput();
   String filename = null;
   filename = input1.input();
   
   /*To Get delimiter to be used from user*/
   GetUserInput input2 = new GetUserInput();
   String user_delimiter = null;
   user_delimiter= input2.delimiter();
   
   /*To Get the output file path from the input excel name*/ 
   File outpath = new File(filename);
   String outputpath = null;
   outputpath = outpath.getParent();
   

   WorkbookSettings ws = new WorkbookSettings();
   ws.setLocale(new Locale("en", "EN"));
   Workbook w = Workbook.getWorkbook(new File(filename),ws);
   
   for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
   {
    /*Objects :
     * 1. To read each sheet 
     * 2. Create output files as per the sheet name in the excel */
    Sheet s = w.getSheet(sheet);
    File f = new File(outputpath+"//output_" + s.getName() + ".txt");
    
    OutputStream os = (OutputStream)new FileOutputStream(f);
    String encoding = "UTF8";
    OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
    BufferedWriter bw = new BufferedWriter(osw);
    
    Cell[] row = null;
    for (int i = 0 ; i < s.getRows() ; i++)
    {
     row = s.getRow(i);
     if (row.length > 0)
     {
      bw.write(row[0].getContents());
      for (int j = 1; j < row.length; j++)
      {
       bw.write(user_delimiter);
       bw.write(row[j].getContents());
      }/*For loop closed*/
     }/*If closed*/         
     bw.newLine();
    }/*Row for loop closed*/
    bw.flush();
    bw.close();
    System.out.print("Output File " + (sheet+1) +  " is created: " + outputpath +"\output_"+ s.getName() +".txt" + "n" );
   }/*Worksheet for loop closed*/      
   System.out.print("Please open the output file(s) using notepad.");   
   
  }/*Try closed*/
  catch (Exception e)
  {
   System.err.println(e);
  }/*Catch closed*/
 }/*Main Closed*/
}/*Class closed*/

[ Read More ]
Read more...

SPLIT HUGE FILES INTO SMALL TEXT FILES

Posted by Raju Gupta at 7:00 AM – 0 comments
 

This code snippet is used for automatic splitting of files conatining large content into smaller text files with specific number of records in each output file. The number of output files depends upon the number of lines to be splitted and the number of lines in the original file.

import java.io.*;
import java.util.Scanner;
public class split {
public static void main(String args[])
{
 try{
  // Reading file and getting no. of files to be generated
  String inputfile = "C:/test.txt"; //  Source File Name.
  double nol = 2000.0; //  No. of lines to be split and saved in each output file.
  File file = new File(inputfile);
  Scanner scanner = new Scanner(file);
  int count = 0;
  while (scanner.hasNextLine()) 
  {
   scanner.nextLine();
   count++;
  }
  System.out.println("Lines in the file: " + count);     // Displays no. of lines in the input file.

  double temp = (count/nol);
  int temp1=(int)temp;
  int nof=0;
  if(temp1==temp)
  {
   nof=temp1;
  }
  else
  {
   nof=temp1+1;
  }
  System.out.println("No. of files to be generated :"+nof); // Displays no. of files to be generated.
 
  //---------------------------------------------------------------------------------------------------------

  // Actual splitting of file into smaller files

  FileInputStream fstream = new FileInputStream(inputfile); DataInputStream in = new DataInputStream(fstream);

  BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine;
 
  for (int j=1;j<=nof;j++)
  {
   FileWriter fstream1 = new FileWriter("C:/New Folder/File"+j+".txt");     // Destination File Location
   BufferedWriter out = new BufferedWriter(fstream1); 
   for (int i=1;i<=nol;i++)
   {
    strLine = br.readLine(); 
    if (strLine!= null)
    {
     out.write(strLine); 
     if(i!=nol)
     {
      out.newLine();
     }
    }
   }
   out.close();
  }

  in.close();
 }catch (Exception e)
 {
  System.err.println("Error: " + e.getMessage());
 }

}

} 


[ Read More ]
Read more...
Tuesday, October 23, 2012

File checksum in Java

Posted by Raju Gupta at 12:00 PM – 0 comments
 
If want to check two files and calculate their hashcode to make sure files are different or same. This function will caluclate the file checksum to compare two files. It will take file as an input.

//To generate a file checksum value with "SHA-1" mechanism 
import java.io.FileInputStream;
import java.security.MessageDigest;
 
public class TestCheckSum {
 
  public static void main(String args[]) throws Exception {
 
    String datafile = "c:\INSTLOG.TXT";
 
    MessageDigest md = MessageDigest.getInstance("SHA1");
    FileInputStream fis = new FileInputStream(datafile);
    byte[] dataBytes = new byte[1024];
 
    int nread = 0; 
 
    while ((nread = fis.read(dataBytes)) != -1) {
      md.update(dataBytes, 0, nread);
    };
 
    byte[] mdbytes = md.digest();
 
    //convert the byte to hex format
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mdbytes.length; i++) {
     sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
    }
 
    System.out.println("Digest(in hex format):: " + sb.toString());
 
  }
}

[ Read More ]
Read more...

Compare two files using Java

Posted by Raju Gupta at 2:11 AM – 0 comments
 

The program will prompt for 2 file names(.txt) that are to be compared. Given the file names, this program will compare the two files.

If any difference is there, it will print the differences and no. of differences in command prompt. 

If no difference is there, it will print
"files are equal".

import java.io.*;
import java.util.*;

//defining class

public class myfilereader
{
//defining main function
 public static void main (String[] args) throws java.io.IOException
    {
  //Getting the name of the files to be compared.
  BufferedReader br2 = new BufferedReader (new
InputStreamReader(System.in));
  System.out.println("Enter 1st File name:");
    String str = br2.readLine();
    System.out.println("Enter 2nd File name:");
    String str1 = br2.readLine();

  String s1="";
    String s2="",s3="",s4="";
    String y="",z="";

        //Reading the contents of the files
   BufferedReader br = new BufferedReader (new FileReader (str));
    BufferedReader br1 = new BufferedReader (new FileReader (str1));

   while((z=br1.readLine())!=null)
    s3+=z;

  while((y=br.readLine())!=null)
     s1+=y;

  System.out.println ();

        //String tokenizing
    int numTokens = 0;
    StringTokenizer st = new StringTokenizer (s1);
    String[] a = new String[10000];
    for(int l=0;l<10000;l++)
      {a[l]="";}
    int i=0;
    while (st.hasMoreTokens())
      {
        s2 = st.nextToken();
          a[i]=s2;
   i++;
            numTokens++;
      }

      int numTokens1 = 0;
     StringTokenizer st1 = new StringTokenizer (s3);
     String[] b = new String[10000];
     for(int k=0;k<10000;k++)
      {b[k]="";}
     int j=0;
     while (st1.hasMoreTokens())
       {
        s4 = st1.nextToken();
        b[j]=s4;
        j++;
        numTokens1++;
       }

//comparing the contents of the files and printing the differences, if any.
  int x=0;
      for(int m=0;m<a.length;m++)
      {
   if(a[m].equals(b[m])){}
    else
    {
     x++;
  System.out.println(a[m] + " -- " +b[m]);
  System.out.println();}
      }
 System.out.println("No. of differences : " + x);
 if(x>0){System.out.println("Files are not equal");}
 else{System.out.println("Files are equal. No difference found");}

 }
}

[ Read More ]
Read more...
Sunday, October 21, 2012

Determining MIME Type of the File

Posted by Raju Gupta at 5:30 AM – 0 comments
 
This is used to determine the MIME type of the file
try {
//Type is used to find the type of the file. It can be "text/plain", "text/xml" ,etc
    String type = Files.probeContentType(filename);
    if (type == null) {
        System.err.format("'%s' has an" + " unknown filetype.%n", filename);
    } else if (!type.equals("text/plain") {
        System.err.format("'%s' is not" + " a plain text file.%n", filename);
        continue;
    }
} catch (IOException x) {
    System.err.println(x);
}


[ Read More ]
Read more...

Taking File name from Absolute Path

Posted by Raju Gupta at 1:10 AM – 0 comments
 
Exact file name extraction is required many times, when dealing with files

public static String extractFileName(String absoluteFileName) {  

 if (!isNullOrEmpty(absoluteFileName)) {  
  //File with the absolute path
  absoluteFileName = absoluteFileName.substring(absoluteFileName  
    .lastIndexOf(File.separatorChar) + 1, absoluteFileName.lastIndexOf("."));  
  //File name extracted the absolute path
 }  
 return absoluteFileName;  
}  

[ Read More ]
Read more...
Saturday, October 20, 2012

Searching for a key in Log Files by connecting to Servers via SSH

Posted by Raju Gupta at 12:59 AM – 0 comments
 

This code helps us to search for the particular key in the log files by connecting to that servers and getting the entries that matches our search key. This can connect to different servers and fetch the results based on our confiuguration.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

/*
*
*Dependency : This java code is dependent on the below jar file which can be downloaded
*Jar Name : ganymed-ssh2-build210.jar
*
*/

public class FilePreviewer {

 private File outputFile;
 private Writer outputStream;
 public static String newline = System.getProperty("line.separator");
 
 public FilePreviewer()
 {
  
 }
 
 public FilePreviewer(String user, String pass)
 {
  
  String ipAd[]={"IP1","IP2"};
  int x=1, numOfServers=x+1;
  String keyText="searchKey"; //search key
  while(x<numOfServers) //incase if we want to search in more than one server in a loop
  {
   try
   {
    Connection connection = new Connection(ipAd[x]);
    connection.setTCPNoDelay(true);
    connection.connect();
    Authentication a=new Authentication(pass);
    System.out.println("Connection Established: "+ipAd[x]);
    outputFile=new File("output_"+x+".txt");//Output File name
    boolean flag=false;
    if(connection.isAuthMethodAvailable(user, "password"))
     flag = connection.authenticateWithPassword(user,pass);
    if(!flag)
    {
     flag = connection.authenticateWithKeyboardInteractive(user, a);
    }
    if (flag == true)
    {
     Session session = connection.openSession();
     String s3 = "grep -i '"+keyText+"' /home/userId/folder/filename.log"; //Configure the search command to point to any file. 
     session.execCommand(s3);
     StreamGobbler streamgobbler = new StreamGobbler(session.getStdout());
     BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(streamgobbler));
     outputStream = new BufferedWriter(new FileWriter(outputFile));
     String cmdOut="";
     int count=0;
     do 
     {
      cmdOut = bufferedreader.readLine();
      if(cmdOut==null)
       break;
      System.out.println(cmdOut);
      outputStream.write(cmdOut+newline);
      count++;
     } while (true);
     outputStream.close();
     session.close();
     
     connection.close();
     if (count==0)
     {
      System.out.println("KeyText not found");
     }
     else
     {
      System.out.println("Found: "+count);
      count=0;
     }
    }
    else
    {
     System.out.println(" Authentication Failed");
    }
   }
   catch(Exception e)
   {
    System.out.println("n"+e.toString());
   }
   x=x+6;
  }
 }
 
 public static void main(String[] args) 
 {
  new FilePreviewer("user","pwd"); //user name and password
 }

}

//Code that takes care of KeyBoard Interactive Authentication

class Authentication implements ch.ethz.ssh2.InteractiveCallback
{
 int promptCount;
 String pwd;

 public Authentication(String password)
 {
  promptCount = 0;
  pwd = password;
 }

 public String[] replyToChallenge(String name, String instruction, int numPrompts, String prompt[], boolean echo[]) throws Exception
 {
  String result[] = new String[numPrompts];
  for(int i = 0; i < numPrompts; i++)
  {
   if(prompt[i].contains("Password:"))
   {
    result[i] = pwd;
    promptCount++;
   }
  }
  return result;
 }
}

[ Read More ]
Read more...
Wednesday, October 17, 2012

File monitoring in a specified file using thread

Posted by Raju Gupta at 9:00 AM – 0 comments
 
This code snippet will continuoudly monitor a specified file. If any text file is pasted in the folder it will fetch the file, reads the file and keep the backup in the specified folder. And delets the file from the current folder.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



/*Monitors the file using threads
*input: File paths
*Output: reading the file, copying to some folder, deleting the file
*/ 
public class MonitorThread implements Runnable {
 public static String outFileName = "/Users/raj/Downloads/temp/output/";
 public static String sourceDirStream = "/Users/raj/Downloads/temp/target/";

 
 private volatile boolean keepRunning = true;
 Thread t;

 private String interval;
 private boolean isExecutingForTheFirstTime = true;
 
 
 public MonitorThread(){
  
  
  t = new Thread(this,"1");
  
  t.start();
 }
 


 public void run()
 {
  while (keepRunning)
  {
   try {
    
    int min =0;
    int sleepTime = 0;
    int hour = 0;
    
    if(keepRunning) {
     
     if(this.isExecutingForTheFirstTime) { 
       min =get_intervalmins();
       hour =get_intervalhour();
       
       sleepTime = (((hour*60)+min)+1)-((hour*60)+min);
       
       Thread.sleep(sleepTime*60000);
       
       isExecutingForTheFirstTime = false; // first time only needed 
       
     }
     MonitorStream();
    
    
    min =get_intervalmins();
    hour =get_intervalhour();
    
    sleepTime = (((hour*60)+min)+1)-((hour*60)+min);
    Thread.sleep(sleepTime*60000);
    
     }
   }
   catch(Exception ex)  {
      ex.printStackTrace();
      keepRunning = false;
   }
   }
  }

 //Functions to get the currentHour and Minutes from System clock. 
 private int get_intervalhour()
 {
  Calendar c = Calendar.getInstance();
  int mins =   c.get(Calendar.MINUTE);
  int hrs =  c.get(Calendar.HOUR_OF_DAY);
  return(hrs);
 }

 private int get_intervalmins()
 {
  Calendar c = Calendar.getInstance();
  int mins =   c.get(Calendar.MINUTE);
  int hrs =  c.get(Calendar.HOUR_OF_DAY);
  return(mins);
 }

 private int get_intervalsecs()
 {
  Calendar c = Calendar.getInstance();
  int secs =   c.get(Calendar.SECOND);
  return(secs);
 }


 private String get_interval(int sleepinterval)
 {
  Calendar c = Calendar.getInstance();
  int mins =   c.get(Calendar.MINUTE);
  int hrs =  c.get(Calendar.HOUR_OF_DAY);
  String strt_interval,end_interval;
 if(sleepinterval == 30)
 {
  if (mins > 15 && mins<=30)
  {
   strt_interval = hrs+":00";
   end_interval =hrs+":15";
   interval =strt_interval+"-"+end_interval;
  }
  if (mins <= 15 && mins != 0 && mins>0)
  {
   int hr = hrs-1;
   if(hr == -1)
   {
    hr= 23;
   }
   strt_interval = hr+":45";
   end_interval = hrs+":00";
   interval=strt_interval+"-"+end_interval;
  }
  if(mins ==0)
  {
   int hr = hrs-1;
   if(hr == -1)
   {
    hr= 23;
   }
   strt_interval = hr+":30";
   end_interval = hr+":45";
   interval=strt_interval+"-"+end_interval;
  }
  
  if(mins>30 && mins<=45)
  {
   strt_interval = hrs+":15";
   end_interval = hrs+":30";
   interval=strt_interval+"-"+end_interval;
   
  }
 }
  
  
 if(sleepinterval ==60)
 {
  if (mins > 30 )
  {
   strt_interval = hrs+":00";
   end_interval =hrs+":30";
   interval =strt_interval+"-"+end_interval;
  }
  if (mins <= 30 && mins != 0)
  {
   int hr = hrs-1;
   if(hr == -1)
   {
    hr= 23;
   }

   strt_interval = hr+":30";
   end_interval = hrs+":00";
   interval=strt_interval+"-"+end_interval;
  }
  if(mins ==0)
  {
   int hr = hrs-1;
   if(hr == -1)
   {
    hr= 23;
   }

   strt_interval = hr+":00";
   end_interval = hr+":30";
   interval=strt_interval+"-"+end_interval;
  }
 }
 
 
 if(sleepinterval ==1440)
 {
  c.add(Calendar.DATE,-1);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  interval = "report for the date " + sdf.format( c.getTime());
 }
  return(interval);
 }

 public  synchronized void MonitorStream()
 {  
  try 
  {
   File localFile = new File(sourceDirStream);
   String[] Stream = localFile.list();
    for(int ii=0; ii< Stream.length ; ii++)
    {
     try{
     System.out.println("file Path = "+sourceDirStream+Stream[ii]);
     displayAndReplaceFile(Stream[ii]);
     deletefile(Stream[ii]);
     }catch(Exception e){
      System.out.println(e);
     
     }
    }
  
  }  
  catch (Exception ftpe) 
  {
   ftpe.printStackTrace();
  }
 }
  
  private static void deletefile(String file){
    File f1 = new File(sourceDirStream+file);
    boolean success = f1.delete();
    if (!success){
    System.out.println(file+" Deletion failed.");
    
    }else{
    System.out.println(file+" File deleted.");
      }
    }
  
  public static void main(String[] args){
   try{
   new MonitorThread();
   }
   catch(Exception e)
   {
    System.out.println("error "+e);
   }
   }
  public void displayAndReplaceFile(String fileName) {

   BufferedReader buffInputFile = null;

   String dataRowFrmFile = null;

   String outFile = getDate() + fileName;
            
   String inpFile = sourceDirStream + fileName;
   System.out.println("File path is : " + inpFile);
   try {
    
    buffInputFile = new BufferedReader(new FileReader(inpFile));
    int count = 0;
    FileWriter fstream = new FileWriter(outFileName+outFile);
    BufferedWriter out1 = new BufferedWriter(fstream);
    System.out.println(" outFileName " + outFileName);
    while ((dataRowFrmFile = buffInputFile.readLine()) != null) {
     out1.write(dataRowFrmFile);
     out1.newLine();
     
     if (count == 0) {
      System.out.println(dataRowFrmFile);
      
     } else {
      System.out.println(dataRowFrmFile);
     }
     count++;
    }
    
    buffInputFile.close();
    buffInputFile = null;
   } catch (Exception e) {

    System.out.println(e.getMessage());
    e.printStackTrace();
   }
  }

  private static String getDate() {
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HHmmss_");
   return sdf.format(new Date());
  }
}


[ Read More ]
Read more...
Tuesday, October 16, 2012

Function to Check Given Date is Older Than 900 days

Posted by Raju Gupta at 6:00 PM – 0 comments
 
The code snippet will results in a boolean using which we can identify that given date is older than 900 days or not.

/**
  * @param args
  * @throws ParseException 
  */
 public static void main(String[] args){
  // TODO Auto-generated method stub
  
  boolean result = false;
  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMyy");
  simpleDateFormat.setLenient(false);
  Date flightDateParsed = null;
  try {
   //Pass the date which we need to check
   flightDateParsed = simpleDateFormat.parse("101111");
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //create Calendar instance
  Calendar olderDate = Calendar.getInstance();
  olderDate.add(Calendar.DATE, -900);
  olderDate.set(Calendar.HOUR_OF_DAY, 0);
  olderDate.set(Calendar.MINUTE, 0);
  olderDate.set(Calendar.SECOND, 0);
  olderDate.set(Calendar.MILLISECOND, 0);
  if (flightDateParsed.before(olderDate.getTime())){
   result = true;
  }
  System.out.println("True = Given Date is Older Than 900 days:::::"+result);
 }


[ Read More ]
Read more...

Transient variable Demo using File Operations

Posted by Raju Gupta at 12:00 PM – 0 comments
 

If a field of an object is transient, when that object is persisted (saved), the value of the transient field will not be persisted.

The TransientDemo class demonstrates this. It contains an inner class, MyDemo, which has a String field named "x" and a transient String field named "y". In the main() method, TransientDemo instatiates a MyDemo object with x set to "normal" and y set to "transient". This object is saved to the file system as a file called "mydemo.ser". We then read this persisted object and get a reference to it called "result". When we display the values of x and y after we've retrieved the object, we can see that the value of x is there but the value of y is null, since it wasn't persisted.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/*This is a demo to show transient variable can't be saved using File serialization. */
public class TransientDemo {

 public static void main(String[] args) {

  class MyDemo implements Serializable {
   private static final long serialVersionUID = 1L;
   String x;
   transient String y;

   MyDemo(String x, String y) {
    this.x = x;
    this.y = y;
   }

   public String toString() {
    return "x is " + x + ", y is " + y;
   }
  }

  try {
   MyDemo md = new MyDemo("normal", "transient");
   System.out.println("Before save: " + md);

   // write object to file
   FileOutputStream fos = new FileOutputStream("mydemo.ser");
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(md);
   oos.close();

   // read object from file
   FileInputStream fis = new FileInputStream("mydemo.ser");
   ObjectInputStream ois = new ObjectInputStream(fis);
   MyDemo result = (MyDemo) ois.readObject();
   ois.close();

   System.out.println("After save: " + result);

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }

}


[ Read More ]
Read more...
Monday, October 15, 2012

Find File in Java

Posted by Raju Gupta at 12:00 AM – 0 comments
 

import java.sql.*;   
import java.text.ParseException;   
import java.text.SimpleDateFormat;   
import java.util.List;   
import java.io.*;   
  
  
class FileFinder   
{   
  static String indirectory = ".";   
  static String filterRegExp = ".*";   
  static String date_time = "01.01.00:00:00";   
  static boolean bFiles = true;   
  static boolean bDirectories = true;   
  
  static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy HH:mm");   
  static final SimpleDateFormat inDateFormat = new SimpleDateFormat("dd.MM.yy:HH:mm");   
  static final String newLine = "rn";   
  
  ////////////////////////////////////////////////////   
  public static void usage()   
  {   
    System.err.println( "Usage: FilesSince <directory> <"filterRegExp"> [-t <date_time>] [-(f|d|b)]");   
    System.err.println( "Options:");   
    System.err.println( "  filterRegExp: Like .* (Everything) or .*\.java (Every file with .java extension)");   
    System.err.println( "  Don't forget to surround the regular expression with quotation marks");   
    System.err.println( "         -t <Minimum file time. Format: dd.mm.yy:hh:mm>");   
    System.err.println( "         -f List files");   
    System.err.println( "         -d List directories");   
    System.err.println( "         -b List both files and directories");   
  }   
  
  
  
  ////////////////////////////////////////////////////   
  static private boolean readCommandLine( String args[])   
  {   
   int i = 0;   
   int argsUsed = 0;   
      
   if( args.length < 3)   
   {   
     usage();   
     return false;   
   }   
  
   while( i < args.length)   
   {   
     if (args[i].charAt(0) == '-')   
     {   
       switch( args[i].charAt(1))   
       {   
         case 'h':   
         case '?':   
           usage();   
           return false;   
         case 't':   
           i++;   
           date_time = args[i];   
           return true;   
         case 'D':   
           bDirectories = true;   
           return true;   
         case 'F':   
           bFiles = true;   
           return true;   
         case 'B':   
           bFiles = true;   
           bDirectories = true;   
           return true;   
         
         default:   
           System.out.println( "*** Unknown option:" + args[argsUsed]);   
           usage();   
           return false;   
         }   
       }   
       else  
       {   
         if( argsUsed==0)        indirectory = args[i];   
         else if ( argsUsed==1)  filterRegExp = args[i];   
         else  
         {   
           System.err.println( "Unknown option:" + args[i]);   
           usage();   
           return false;   
         }   
         argsUsed++;   
       }   
     i++;   
   }   
   return true;   
  }   
     
  ////////////////////////////////////////////////////   
  static boolean initialize()   
  {   
    return true;   
  }   
  
/*    
  static void recurse( Vector vecFiles, String actPath, long minDate) throws IOException  
  {  
    String pathname;   
    File f = new File( actPath);  
 
    File[] a_f = f.listFiles();  
      
    for( int i=0; i<a_f.length; i++)  
    {  
      pathname = a_f[i].getCanonicalPath();  
      if( a_f[i].isDirectory())  
      {  
        recurse( vecFiles, pathname, minDate);  
      }  
      else  
      {  
        if( a_f[i].lastModified() > minDate )  
        {  
          vecFiles.add( a_f[i]);  
        }  
      }  
    }  
  }  
*/  
     
 /****************************************************************************  
 * main  
 *****************************************************************************/  
 public static void main (String args []) throws SQLException   
 {   
   if (!readCommandLine( args))   
   {   
     return; //>>>>>>>>>>>   
   }   
   if( indirectory == null)   
   {   
     usage();   
     return; //>>>>>>>>>>>   
   }   
      
   final long minDate;   
   try    
   {   
     minDate = inDateFormat.parse( date_time).getTime();   
   }    
   catch (ParseException e1)    
   {   
     e1.printStackTrace();   
     usage();   
     return; //>>>>>>>>>>>   
   }   
  
   List files;   
   try {   
     indirectory = new File( indirectory).getCanonicalPath();   
     files = FileUtils.getFileListing( new File( indirectory), filterRegExp, true, false);   
   }    
   catch (FileNotFoundException e) {   
     e.printStackTrace();   
     return;   
   }   
   catch (IOException e) {   
     e.printStackTrace();   
     return;   
   }   
  
   // print out all file names, and display the order of File.compareTo   
   for( int i=0; i<files.size(); i++)   
   {   
     File file = (File) files.get(i);   
     if( file.lastModified() > minDate)   
     {   
       System.out.println( file);   
     }   
   }   
  
/*     
   Vector v = new Vector();   
   try   
   {  
     recurse( v, indirectory, minDate);  
   }   
   catch (IOException e1)   
   {  
     e1.printStackTrace();  
     return;  
   }  
     
   for( int i=0; i<v.size(); i++)  
   {  
//       System.out.println( ((File)v.elementAt(i)).getCanonicalPath());  
      System.out.println( ((File)v.elementAt(i)).getPath());  
   }  
*/     
  }   
}  
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.io.*;


class FileFinder
{
  static String indirectory = ".";
  static String filterRegExp = ".*";
  static String date_time = "01.01.00:00:00";
  static boolean bFiles = true;
  static boolean bDirectories = true;

  static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy HH:mm");
  static final SimpleDateFormat inDateFormat = new SimpleDateFormat("dd.MM.yy:HH:mm");
  static final String newLine = "rn";

  ////////////////////////////////////////////////////
  public static void usage()
  {
    System.err.println( "Usage: FilesSince <directory> <"filterRegExp"> [-t <date_time>] [-(f|d|b)]");
    System.err.println( "Options:");
    System.err.println( "  filterRegExp: Like .* (Everything) or .*\.java (Every file with .java extension)");
    System.err.println( "  Don't forget to surround the regular expression with quotation marks");
    System.err.println( "         -t <Minimum file time. Format: dd.mm.yy:hh:mm>");
    System.err.println( "         -f List files");
    System.err.println( "         -d List directories");
    System.err.println( "         -b List both files and directories");
  }



  ////////////////////////////////////////////////////
  static private boolean readCommandLine( String args[])
  {
   int i = 0;
   int argsUsed = 0;
   
   if( args.length < 3)
   {
     usage();
     return false;
   }

   while( i < args.length)
   {
     if (args[i].charAt(0) == '-')
     {
       switch( args[i].charAt(1))
       {
         case 'h':
         case '?':
           usage();
           return false;
         case 't':
           i++;
           date_time = args[i];
           return true;
         case 'D':
           bDirectories = true;
           return true;
         case 'F':
           bFiles = true;
           return true;
         case 'B':
           bFiles = true;
           bDirectories = true;
           return true;
      
         default:
           System.out.println( "*** Unknown option:" + args[argsUsed]);
           usage();
           return false;
         }
       }
       else
       {
         if( argsUsed==0)        indirectory = args[i];
         else if ( argsUsed==1)  filterRegExp = args[i];
         else
         {
           System.err.println( "Unknown option:" + args[i]);
           usage();
           return false;
         }
         argsUsed++;
       }
     i++;
   }
   return true;
  }
  
  ////////////////////////////////////////////////////
  static boolean initialize()
  {
    return true;
  }

/*  
  static void recurse( Vector vecFiles, String actPath, long minDate) throws IOException
  {
    String pathname; 
    File f = new File( actPath);

    File[] a_f = f.listFiles();
    
    for( int i=0; i<a_f.length; i++)
    {
      pathname = a_f[i].getCanonicalPath();
      if( a_f[i].isDirectory())
      {
        recurse( vecFiles, pathname, minDate);
      }
      else
      {
        if( a_f[i].lastModified() > minDate )
        {
          vecFiles.add( a_f[i]);
        }
      }
    }
  }
*/
  
 /****************************************************************************
 * main
 *****************************************************************************/
 public static void main (String args []) throws SQLException
 {
   if (!readCommandLine( args))
   {
     return; //>>>>>>>>>>>
   }
   if( indirectory == null)
   {
     usage();
     return; //>>>>>>>>>>>
   }
   
   final long minDate;
   try 
   {
     minDate = inDateFormat.parse( date_time).getTime();
   } 
   catch (ParseException e1) 
   {
     e1.printStackTrace();
     usage();
     return; //>>>>>>>>>>>
   }

   List files;
   try {
     indirectory = new File( indirectory).getCanonicalPath();
     files = FileUtils.getFileListing( new File( indirectory), filterRegExp, true, false);
   } 
   catch (FileNotFoundException e) {
     e.printStackTrace();
     return;
   }
   catch (IOException e) {
     e.printStackTrace();
     return;
   }

   // print out all file names, and display the order of File.compareTo
   for( int i=0; i<files.size(); i++)
   {
     File file = (File) files.get(i);
     if( file.lastModified() > minDate)
     {
       System.out.println( file);
     }
   }

/*   
   Vector v = new Vector(); 
   try 
   {
     recurse( v, indirectory, minDate);
   } 
   catch (IOException e1) 
   {
     e1.printStackTrace();
     return;
   }
   
   for( int i=0; i<v.size(); i++)
   {
//       System.out.println( ((File)v.elementAt(i)).getCanonicalPath());
      System.out.println( ((File)v.elementAt(i)).getPath());
   }
*/  
  }
}

[ Read More ]
Read more...
Sunday, October 14, 2012

Updating Permission of a file using java program on Unix Operation System

Posted by Raju Gupta at 2:00 AM – 0 comments
 
This program will help you modify the permission of a file.on Unix operating system. You need to pass the absolute path of the file and the permission(e.g. 775,700,777 etc.) you wnat to give, as the command line parameters.

import java.io.*;
import java.util.*;

/**
 ** The FilePerm class has a method updateFilePerm
 ** to assist in changing the permission of a file on Unix OS,
 ** through java program
 **/


public class FilePerm {


    /** updateFilePerm method coverts permission of file 
     *  by using chmod command which allow either of -rwx- 
     *  permission to the file. 
     *
     */
   
    public static boolean updateFilePerm(String fileName, int mode)        
            throws Exception {   
 
 String method = "updateFilePerm";
 boolean updateError = false;
 
 /** retry concept */
        int retry = 0;
 
 /** changeMod */
        String changemod = null;
 
        
 /** Runtime class object */
        Runtime rt = null;
 
        /** Process class object */
        Process proc = null;  
       
 if (null == fileName || ("").equals(fileName)) {
                throw new Exception("Source File is null:Null pointer Exception");
        }
  if (mode < 0 || mode > 777) {
                throw new Exception("Invalid Mode");
         }
 try {     
     rt= Runtime.getRuntime();
                     
     changemod = "chmod" + " " + mode + " " + fileName;
     do {         
  /** Changing the mode of the file */
  try {
      proc = rt.exec(changemod); 
      proc.waitFor();
  } catch (IOException ioex) {                     
                     throw ioex;
  }
         /** In case of abnormal termination of proecss */
  if(proc.exitValue() != 0 ){
          updateError=true;
      retry++;
  } else { 
      updateError = false;
  }
     } while (updateError && retry < 3);
 
     return updateError; 
     
            }catch(Exception ex){

            throw ex;
        }
  
    }
    
    public static void main(String[] args) {
       try {
    updateFilePerm(args[0],Integer.parseInt(args[1]));  
       } catch(Exception Ex) {
           System.out.println("Exception in giving permission to the file :"+Ex);
       }      
    } 
       
}


[ Read More ]
Read more...

Change in accessibilty of File

Posted by Raju Gupta at 1:00 AM – 0 comments
 
When we are transfering files from one platform to another for eg: windows to unix or vice versa , in that case there will be default mode assigned to the transfered file . so to change the mode of the transfered file we can use the "Change in accessibility of file" code snippet in our logic.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class updateFilePerm {
 public static void main(String[] args) {
  boolean updateError = false;
  int retry = 0;
  String changemod = null;
  Runtime rt = null;
  Process proc = null;
  Scanner in = new Scanner(System.in);
  System.out.println("Enter the FileName");
  String fileName = in.nextLine();
  File file = new File(fileName);
  boolean b = false;
  b = file.exists();
  if (!b) {
   System.out.println("File doesnot Exist");
   System.exit(1);
  } else {
   System.out.println("Enter Mode");
   int mode = in.nextInt();

   if (null == fileName || ("").equals(fileName)) {
    System.out.println("FielName cannot be null ");
    System.exit(1);
   }
   if (mode < 0 || mode > 777) {
    System.out.println("Invalid Mode entered");
    System.exit(1);
   }
   try {
    rt = Runtime.getRuntime();

    changemod = "chmod" + " " + mode + " " + fileName;
    do {
     /** Changing the mode of the file */
     try {
      proc = rt.exec(changemod);
      proc.waitFor();
     } catch (IOException se) {
      System.out.println(se.toString());
     }
     /** In case of abnormal termination of process */
     if (proc.exitValue() != 0) {
      updateError = true;
      retry++;
     } else {
      updateError = false;
     }
    } while (updateError && retry < 3);
   } catch (Exception ex) {
    System.out.println(ex.toString());
   }
  }

 }

}


[ Read More ]
Read more...
Saturday, October 13, 2012

Java Code of copying and pasting a file

Posted by Raju Gupta at 2:00 PM – 0 comments
 
This code contains simple logic of copy paste of a file content

import java.io.*;
public class FileMain 
{
 public static void main(String []args) throws IOException

 {    
  FileInputStream f1= new FileInputStream("/give_file_name.txt");// creats an input stream  which points a file
  OutputStream f2= new FileOutputStream("/a.txt");// creats out put stream   int i= f1.available();// checks sise of the file
  // System.out.println(i);
  for(int q=0;q<=i;q++ )
  {  
   f2.write((char)f1.read());
  }

 }
}


[ Read More ]
Read more...
Wednesday, October 10, 2012

How to read the content of a flat file from JSP

Posted by Raju Gupta at 11:29 AM – 0 comments
 
As like in Java code we canĂ¢€™t hardcode the filenames in JSP. Because the application path differ from server to server and it needs to be generated dynamically. So getting the application path at run time is the key point in reading a flat file from JSP. Once we get the application path the remaining part is similar to Java code.
The method that is used to get the application path is getRealPath. This is available in the interface ServletContext. We can access this method in JSP using implicit object, Application.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Question & Answer Page</title>
</head>
<body>

<h2><center>Welcome to Questionnaire Forum....!</center></h2>
<br/><br/>



<%@ page import = "java.lang.*,java.io.*,java.util.*" %>
<%


// variables for connecting to the flat file data source
// Note: this is set up to look in the same directory as the jsp file.


String fileName = "question_answer.txt";
BufferedReader br = null;
FileReader fr;

String inputPath = application.getRealPath("/") + File.separatorChar;

try {

// open a bufferedreader to read the file
// get the current path of the jsp under the application server.
// 'application' is a freebie from the parent servlet.



      fr = new FileReader (new File(inputPath + fileName));
      br = new BufferedReader (fr);
      String line = br.readLine();

      while (line != null) {
 
 out.println(line);
%>

<br/>

<%
 line = br.readLine();
    }
 
 br.close();



   } 

catch (Exception e) {


}


%>


</body>
</html>

[ Read More ]
Read more...
Saturday, September 15, 2012

Program for multithreaded file reading

Posted by Admin at 6:14 AM – 0 comments
 

import java.io.*;
import java.lang.*;
class MultiThreadedFileRead extends Thread
{
        InputStream in;
        MultiThreadedFileRead(String fname) throws Exception
        {
                in=new FileInputStream(fname);
                this.start();
        }
        public void run()
        {
                int i=0;
                while(i!=-1)
                {
                        try
                        {
                                i=in.read();
                                System.out.print((char)i);
                        }catch(Exception e){}
                }
                try
                {
                        in.close();
                }catch(Exception e){}
        }
        public static void main(String a[]) throws Exception
        {
                int n=2;
                System.out.print("Enter the number of files : ");
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                try
                {
                        n=Integer.parseInt(br.readLine());
                }catch(Exception e){}
                MultiThreadedFileRead fr[]=new MultiThreadedFileRead[n];
                long tim;
                tim=System.currentTimeMillis();
                for(int i=0;i<n;i++)
                        fr[i]=new MultiThreadedFileRead(a[i]);
                for(int i=0;i<n;i++)
                {
                        try
                        {
                                fr[i].join();
                        }catch(Exception e){}
                }
                System.out.println("Time Required : "+(System.currentTimeMillis()-tim)+" miliseconds.");
        }
}

[ Read More ]
Read more...

Java Read File Line by Line

Posted by Admin at 5:40 AM – 0 comments
 


import java.io.*;
class FileRead 
{
 public static void main(String args[])
 {
  try{
   // Open the file that is the first 
   // command line parameter
   FileInputStream fstream = new FileInputStream("textfile.txt");
   // Get the object of DataInputStream
   DataInputStream in = new DataInputStream(fstream);
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String strLine;
   //Read File Line By Line
   while ((strLine = br.readLine()) != null)   {
    // Print the content on the console
    System.out.println (strLine);
   }
   //Close the input stream
   in.close();
  }catch (Exception e){//Catch exception if any
   System.err.println("Error: " + e.getMessage());
  }
 }
}



[ Read More ]
Read more...

Archive or Delete Files after a Certain Time Period

Posted by Admin at 1:08 AM – 0 comments
 
The Java Program will either archive files or delete files that are older than a specified period (RetentionPeriodInDays).

List of folders to be purged / archived is configurable and is mentioned in a XML config file

If operation is 'archive', compress old files to *.tar.gz and move to Archive folder.
If operation is 'delete', delete files older than 'retention period'.

package service;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import directory.generated.Folder;
import directory.generated.Folders;
import directory.generated.OperationType;

/**
 * 
 * 
 *         IF OPERATION IS ARCHIVE, COMPRESS OLD FILES TO *.TAR.GZ AND MOVE TO
 *         ARCHIVE FOLDER.
 * 
 *         IF OPERATION IS DELETE, DELETE FILES OLDER THAN 'RETENTION PERIOD'.
 * 
 *         LIST OF FOLDERS TO BE PURGED/ARCHIVED IS MENTIONED IN A XML CONFIG
 *         FILE
 * 
 */
public class HouseKeeping {
 // CONFIG FILE LOCATION, TODO GET FROM RESOURCE BUNDLE
 private static final String configFile = "src/config/Sample.xml";

 private Logger logger = LoggerFactory.getLogger(this.getClass()
   .getSimpleName());
 private Date currDate = new Date();
 private String pathToBeExcluded = "";

 /**
  * Main method
  * 
  * @throws Exception
  */
 public void houseKeepingService() throws Exception {
  logger.info("HOUSE KEEPING PROCESS - START");

  // VARIABLES FROM XML
  String folderId = null;
  String srcDir = null;
  String archDir = null;
  Integer retentionPeriod;

  String tarFileName = null;

  // READ XML CONFIG FILE & UNMARSHALL; OBTAIN LIST OF FOLDERS
  JAXBContext jc = JAXBContext.newInstance(Folders.class);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  Folders config = (Folders) unmarshaller.unmarshal(new FileReader(
    new File(configFile)));
  List folders = config.getFolder();

  // LOOP THRU THE FOLDER LIST
  for (Folder folder : folders) {
   folderId = folder.getId();
   logger.info("PROCESSING - " + folderId);

   srcDir = folder.getFolderName();
   if (isStringEmpty(srcDir)) {
    logger.error("SRC DIRECTORY NOT SPECIFIED FOR " + folderId);
    continue;
   }

   retentionPeriod = folder.getRetentionPeriodInDays();
   logger.debug("RETENTION PERIOD - " + retentionPeriod);

   archDir = folder.getArchivalFolder();

   // CHECK FOR DELETE / ARCHIVE
   if (folder.getOperation().equals(OperationType.DELETE)) {
    logger.debug("DELETE: CHECKING FOR OLD FILES IN DIR - "
      + srcDir);
    try {
     dataRetention(new File(srcDir), retentionPeriod);
    } catch (IOException ioEx) {
     logger.error("error deleting old files for " + folderId,
       ioEx);
    }

   } else if (folder.getOperation().equals(OperationType.ARCHIVE)) {
    /*
     * COMPRESS OLD FILES TO *.TAR.GZ AND MOVE TO ARCHIVE FOLDER.
     * ALSO DELETE THE FILES ADDED TO THE ARCHIVE.
     */
    try {
     logger.debug("ARCHIVE: CHECKING FOR OLD FILES IN DIR - "
       + srcDir);
     // CREATE TAR FILE
     tarFileName = archiveFiles(srcDir, archDir,
       retentionPeriod, folderId);
     if (isStringEmpty(tarFileName)) {
      logger.warn("ARCHIVAL PROCESS FAILED FOR " + folderId);
     } else {
      // GZIP FILE
      gzipCompressFile(tarFileName);
      // DELETE TAR FILE AFTER CREATION OF GZIP
      deleteFile(new File(tarFileName));
     }
    } catch (FileNotFoundException fileEx) {
     logger.error("file not found " + tarFileName, fileEx);
    } catch (ArchiveException archiveEx) {
     logger.error("housekeeping failed for " + folderId,
       archiveEx);
    } catch (IOException ioEx) {
     logger.error("housekeeping failed for " + folderId, ioEx);
    }
   } else {
    // INVALID OPTION
    logger.warn("INVALID OPERATION. SHOULD BE DELETE / ARCHIVE");
   }

   // CONTINUE WITH NEXT RULE IF ANY
  }

  logger.info("HOUSE KEEPING PROCESS - COMPLETED");
 }

 /**
  * CRAETE A TAR FILE
  * 
  * @param strSrcDir
  *            src dir ending with File.separator
  * @param strArchDir
  *            target archive dir ending with File.separator. tar files will
  *            be created here.
  * @param archivalPeriod
  * @throws FileNotFoundException
  * @throws ArchiveException
  * @throws IOException
  */
 public String archiveFiles(String strSrcDir, String strArchDir,
   Integer archivalPeriod, String folderId)
   throws FileNotFoundException, ArchiveException, IOException {

  // VALIDATIONS!
  if (archivalPeriod == null) {
   logger.warn("ARCHIVAL PERIOD NOT SPECIFIED FOR " + folderId);
   return null;
  }

  if (isStringEmpty(strSrcDir)) {
   logger.error("SRC DIRECTORY IS MISSING FOR " + folderId);
   return null;
  }

  if (isStringEmpty(strArchDir)) {
   logger.error("ARCHIVE DIRECTORY NOT SPECFIED FOR " + folderId
     + ". USE INPUT DIR AS ARCHIVE DIR.");
   // SO ARCHIVE IN THE SRC DIR
   strArchDir = strSrcDir;
  }

  File srcDir = new File(strSrcDir);
  if (!srcDir.exists()) {
   logger.warn("SKIP ARCHIVAL... INVALID SOURCE DIRECTORY "
     + strSrcDir);
   return null;
  }

  File archDir = new File(strArchDir);
  if (!archDir.exists()) {
   // AIX UNIX - FOLDER WILL GET CREATED AUTOMATICALLY. BUT
   // IN WINDOWS - ERROR!
   logger.error("ARCHIVE DIRECTORY DOES NOT EXIST " + archDir);
   return null;
  }

  // ARCHIVE

  pathToBeExcluded = srcDir.getCanonicalPath();
  logger.debug("pathToBeExcluded " + pathToBeExcluded);

  File[] dirList = null; // LIST OF FILES IN SRC DIR
  if (srcDir.isDirectory()) {
   logger.debug("LOOKING FOR FILES IN " + srcDir.getCanonicalPath());
   dirList = srcDir.listFiles();

   if (dirList.length == 0) {
    logger.warn("NO FILES FOUND IN DIR - " + strSrcDir);
    return null;
   }
  } else {
   // JUST ADD THE SINGLE FILE TO TAR
   dirList = new File[] { srcDir };
   pathToBeExcluded = srcDir.getCanonicalPath().substring(0,
     srcDir.getCanonicalPath().lastIndexOf(File.separator));
  }

  // TAR FILE NAME WILL BE CURRENT DATE & TIMESTAMP
  String tarFileName = strArchDir + getCurrentDateTime("yyyyMMddhhmm")
    + ".tar";
  logger.debug("creating tarFile.... " + tarFileName);
  File tarFile = new File(tarFileName);
  tarFileName = tarFile.getCanonicalPath();
  OutputStream out = null;
  TarArchiveOutputStream aos = null;

  try {
   out = new FileOutputStream(tarFile);
   aos = (TarArchiveOutputStream) new ArchiveStreamFactory()
     .createArchiveOutputStream(ArchiveStreamFactory.TAR, out);

   // LOOP THRU LIST OF FILES IN SRC DIR
   for (File file : dirList) {
    logger.trace("FileName:" + file.getCanonicalPath());
    if (file.isDirectory()) {
     // CHK SUB FOLDERS FOR FILES & ADD TO TAR
     tarDir(file, aos, archivalPeriod);
    } else if (file.isFile()) {
     // TODO CHEK FOR FILENAME PATTERN
     if (isFileOld(file, archivalPeriod)) {
      tarFile(file, aos);
      // DELETE THE FILE WHICH HAS BEEN ADDED TO TAR
      deleteFile(file);
     }
    }
   }
   logger.info("TAR FILE CREATED SUCCESSFULLY - " + tarFileName);
  } finally {
   this.finish(aos);
   this.close(aos);
   this.close(out);
  }
  return tarFileName;
 }

 /**
  * DELETE FILES OLDER THAN SPECIFED RETENTION PERIOD
  * 
  * @param deleteDir
  * @param retentionPeriod
  * @throws IOException
  */
 private void dataRetention(File deleteDir, Integer retentionPeriod)
   throws IOException {
  if (!deleteDir.exists()) {
   logger.warn("SKIP RETENTION... INVALID DIRECTORY TO DELETE FILE FROM -"
     + deleteDir);
   return;
  }

  if (retentionPeriod == null) {
   logger.warn("SKIP RETENTION... RETENTION PERIOD NOT SPECIFIED");
   return;
  }

  logger.info("CHECKING FOR OLD FILES IN DIR - "
    + deleteDir.getCanonicalPath());

  File[] dirList = deleteDir.listFiles();
  if (dirList.length == 0) {
   logger.info("NO FILES FOUND IN " + deleteDir.getCanonicalPath());
   return;
  }
  for (File file : dirList) {
   logger.trace("processing file:" + file);
   if (file.isDirectory()) {
    // DO NOT DELETE DIRECTORIES!!!!
    // RECURSIVELY CHECK SUB-DIRECTORIES FOR OLDER FILES
    dataRetention(file, retentionPeriod);
   } else if (file.isFile()) {
    // TODO CHEK FOR FILENAME PATTERN
    if (isFileOld(file, retentionPeriod)) {
     deleteFile(file);
    }
   }
  }

 }

 /**
  * 
  * @param file
  *            file to be verified
  * @param retentionPeriod
  *            number of days to retain files
  * @return
  * @throws IOException
  */
 private boolean isFileOld(File file, int retentionPeriod)
   throws IOException {
  Date lastModifiedDate;
  lastModifiedDate = new Date(file.lastModified());
  logger.trace(file + " last modified on " + lastModifiedDate);
  int diffInDays = getDifferenceInDays(currDate, lastModifiedDate);
  logger.trace("DIFF IN DAYS:" + diffInDays);

  if (diffInDays > retentionPeriod) {
   logger.trace(file.getCanonicalPath() + " IS OLD. DAYS-"
     + diffInDays);
   return true;
  }
  return false;
 }

 /**
  * ADD FILE TO TAR ARCHIVE
  * 
  * @param reqFile
  *            file to be verified
  * @param aos
  * @param retentionPeriod
  *            number of days to retain files
  * @throws IOException
  */
 private void tarDir(File reqFile, TarArchiveOutputStream aos,
   int retentionPeriod) throws IOException {
  logger.debug("looking for files in directory "
    + reqFile.getCanonicalPath());
  File[] dirList = reqFile.listFiles();

  // TODO - FIX EMPTY DIR CREATION IN TAR (NO ELIGIBLE FILE IS PRESENT).

  // ADD A EMPTY DIRECTORY
  addDirectoryToTar(reqFile, aos);

  for (File file : dirList) {
   logger.trace("processing file:" + file);
   if (file.isDirectory()) {
    // RECURSIVELY CHECK FOR FILES IN DIR
    tarDir(file, aos, retentionPeriod);
   } else if (file.isFile()) {
    if (isFileOld(file, retentionPeriod)) {
     // ADD FILE TO TAR
     tarFile(file, aos);
     // DELETE THE FILE WHICH HAS BEEN ADDED TO TAR
     deleteFile(file);
    }
   }
  }

 }

 /**
  * 
  * @param file
  * @param aos
  * @throws IOException
  */
 private void addDirectoryToTar(File file, TarArchiveOutputStream aos)
   throws IOException {
  try {
   String dirName = getFileName(file);
   logger.debug("ADD DIR TO TAR - " + dirName);
   TarArchiveEntry entry = new TarArchiveEntry(file, dirName);
   aos.putArchiveEntry(entry);
   aos.closeArchiveEntry();
  } catch (IOException ioEx) {
   logger.warn("error adding dir to tar -" + file.getCanonicalPath(),
     ioEx);
   throw ioEx;
  }
 }

 /**
  * 
  * @param file
  * @param aos
  * @throws IOException
  */
 private void tarFile(File file, TarArchiveOutputStream aos)
   throws IOException {
  TarArchiveEntry entry;
  FileInputStream fin = null;
  try {
   String fileName = getFileName(file);
   logger.debug("ADD FILE TO TAR - " + fileName);
   entry = new TarArchiveEntry(file, fileName);
   entry.setSize(file.length());
   aos.putArchiveEntry(entry);
   fin = new FileInputStream(file);
   IOUtils.copy(fin, aos);
   aos.closeArchiveEntry();
  } catch (IOException ioEx) {
   logger.warn("error adding file to tar -" + file.getCanonicalPath(),
     ioEx);
   throw ioEx;
  } finally {
   this.close(fin);
  }
 }

 /**
  * 
  * @param srcFileName
  *            file to gzipped!
  * @throws FileNotFoundException
  * @throws IOException
  */
 private void gzipCompressFile(String srcFileName)
   throws FileNotFoundException, IOException {
  OutputStream out = null;
  InputStream fin = null;
  GzipCompressorOutputStream gzip = null;
  String gzipFileName = srcFileName + ".gz";
  logger.debug("CREATING GZIP FILE..... " + gzipFileName);

  try {
   File srcFile = new File(srcFileName);
   if (!srcFile.exists() || !srcFile.isFile()) {
    logger.error("INVALID FILE - " + srcFileName);
    return;
   }

   fin = new FileInputStream(srcFile);
   out = new FileOutputStream(gzipFileName);
   gzip = new GzipCompressorOutputStream(out);
   IOUtils.copy(fin, gzip);
   logger.info("SUCCESSFULLY CREATED GZIP FILE - " + gzipFileName);
  } finally {
   this.close(fin);
   this.close(gzip);
   this.close(out);
  }

 }

 /**
  * DELETE SPECIFIED FILE
  * 
  * @param file
  *            file to be deleted
  * @throws IOException
  */
 public void deleteFile(File file) throws IOException {
  String strCanonicalPath = file.getCanonicalPath();
  logger.debug("DELETING FILE " + strCanonicalPath);
  // DO NOT DELETE ANY DIRECTORIES
  if (!file.exists() || !file.isFile()) {
   logger.error("INVALID FILE " + strCanonicalPath);
   return;
  }
  boolean deleteStatus = file.delete();
  if (!deleteStatus) {
   logger.error("FAILED TO DELETE FILE " + strCanonicalPath);
  }
 }

 /**
  * derive file name to be used in tar. basically removes the directory path.
  * 
  * @param file
  * @return file name to be used in tar
  * @throws IOException
  */
 private String getFileName(File file) throws IOException {
  // filename to be used in tar
  String strCanonicalPath = file.getCanonicalPath();
  String fileName = "";
  // INDEX OF SRC
  int index = strCanonicalPath.indexOf(pathToBeExcluded);
  if (index >= 0) {
   fileName = strCanonicalPath.substring(index
     + pathToBeExcluded.length() + 1);
  }
  logger.trace("Name to be used in TAR-" + fileName);
  return fileName;
 }

 // ************ COMMON UTILITY FUNCTIONS ****************

 /**
  * CLOSE STREAM
  * 
  * @param stream
  * @throws IOException
  */
 private void close(Closeable stream) throws IOException {
  try {
   if (stream != null) {
    stream.close();
   }
  } catch (IOException ioEx) {
   logger.warn("error closing stream", ioEx);
   throw ioEx;
  }
 }

 /**
  * Ends the TAR archive without closing the underlying OutputStream
  * 
  * @param aos
  * @throws IOException
  *             on error
  */
 private void finish(TarArchiveOutputStream aos) throws IOException {
  try {
   if (aos != null) {
    aos.finish();
   }
  } catch (IOException ioEx) {
   logger.warn("error while finish TarArchiveOutputStream", ioEx);
   throw ioEx;
  }
 }

 /**
  * This method checks whether the string is empty or not
  * 
  * @param strToCheck
  *            the string to be checked
  * @return it returns true if the string is null or empty, otherwise it
  *         returns false
  */
 public static boolean isStringEmpty(String strToCheck) {
  if ((null == strToCheck) || strToCheck.trim().isEmpty()) {
   return true;
  } else {
   return false;
  }
 }

 /**
  * get the difference in days between two dates. considers DST.
  * 
  * @param date1
  *            the first date
  * @param date1
  *            the second date
  * 
  * @return int the difference in days (-ve if date1 < date2)
  * @throws ParseException
  */
 public static int getDifferenceInDays(Date date1, Date date2) {

  Calendar cal1 = Calendar.getInstance();
  cal1.setTime(date1);

  Calendar cal2 = Calendar.getInstance();
  cal2.setTime(date2);

  // HAVE TO CONVERT BOTH THE INPUT DATES TO GMT TIMEZONE

  // cal.getTimeZone().getOffset(cal.getTimeInMillis()) -> amount of time
  // in milliseconds to add to GMT to get local time

  long timeDiff = (cal1.getTimeInMillis() + cal1.getTimeZone().getOffset(
    cal1.getTimeInMillis()))
    - (cal2.getTimeInMillis() + cal2.getTimeZone().getOffset(
      cal2.getTimeInMillis()));
  return (int) TimeUnit.MILLISECONDS.toDays(timeDiff);

  // long timeDiff = Math.abs(date1.getTime() - date2.getTime());
  // return (int) TimeUnit.MILLISECONDS.toDays(timeDiff);
 }

 /**
  * get current date
  * 
  * @param dateFormat
  * @return
  */
 public static String getCurrentDateTime(String dateFormat) {
  Date today = new Date();
  DateFormat formatter = new SimpleDateFormat(dateFormat);
  return formatter.format(today);
 }

 public static void main(String[] args) {

  HouseKeeping tester = new HouseKeeping();
  try {
   tester.houseKeepingService();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

[ Read More ]
Read more...

Append a text to file in Java

Posted by Admin at 12:55 AM – 0 comments
 

BufferedWriter out = null;
try {
    out = new BufferedWriter(new FileWriter("€filename"€, true));
    out.write("String");
} catch (IOException e) {
    // error processing code
} finally {
    if (out != null) {
        out.close();
    }
}

[ Read More ]
Read more...
Friday, September 14, 2012

Repalce Word from the Given File Java Example

Posted by Admin at 2:00 PM – 0 comments
 

If we pass a file path and a word and the replace word to a method then it will replace all the word in that file with replace word.


/************************
 * Code to replace all word in a test file 
 ************************/
import java.io.*;

public class repalceWord {
        /*Repalces words in a file*/
 
 public static void contentReplace(String filepath,String word,String replaceword) {
  //String filepath="D:/test.txt";
  //String word="something";
  //String replaceword="evertthing";
  String data="";
  File file=new File(filepath);
  try{
                    
      FileInputStream fis = new FileInputStream(file);/*creates a new file input stream */
                    
      InputStreamReader is = new InputStreamReader(fis);/*Creates a new file input stream*/
                    
      BufferedReader br = new BufferedReader(is);/*creates a buffer reader*/
      String strLine="";
                    
      while ((strLine = br.readLine()) != null)/*checks if their are any lines in a file
           {
                                
        if(strLine.contains(word)){ /*checks if the file contains such a word*/
                                         
         data+=strLine.replaceAll(word, replaceword)+"n";/*It replaces all the words in the file*/
        }else{
                                        
         data+=strLine+"n";/*If no such word it moves to next line*/
        }
           }
      System.out.println(data);
      br.close();
      is.close();
   fis.close();
   FileOutputStream fileOutputStream=new FileOutputStream(filepath,false);/*creates a new file output stream */
   Writer outx=new OutputStreamWriter(fileOutputStream);/*creates a Writer for the file output stream */
    
   if(data.contains("n")){ 
    String[] list=data.split("n");/*split the data*/
    for(int i=0;i<list.length;i++){
     
     outx.write(list[i]);
     outx.append("n");
     
    } 
   }else{
    outx.write(data);
    outx.append("n");
   }
   outx.close();
   fileOutputStream.close();
      
  }catch(Exception e){
   e.printStackTrace();
  }
  
 }
}

[ Read More ]
Read more...
Older Posts
Subscribe to: Posts ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger