Showing posts with label Java Utilities. Show all posts
Showing posts with label Java Utilities. Show all posts

Thursday, October 15, 2015

Java Copy directory recursive




.

 package apache.commons;  
 import java.io.File;  
 import java.io.IOException;  
 import org.apache.commons.io.FileUtils;  
 public class CopyDirectory {  
      public static void main(String[] args) {  
           new CopyDirectory().copyDirectory();  
           new CopyDirectory().checkCopy();  
      }  
      public void copyDirectory() {  
           File srcDir = new File("/Users/S_Das/Documents/Java/project.chanko");  
           File destDir = new File("/Users/S_Das/Documents/Java/new.project.chanko");  
           try {  
                FileUtils.copyDirectory(srcDir, destDir);  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
      public void checkCopy() {  
           File file = new File("/Users/S_Das/Documents/Java/new.project.chanko");  
           if (file.exists()) {  
                System.out.println("Directory copied");  
           }  
           else {  
                System.out.println("not");  
           }  
      }  
 }  

Java Copy file using NIO





 package FileSystem;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.nio.file.Files;  
 import java.nio.file.Paths;  
 import java.nio.file.StandardCopyOption;  
 public class CopyFiles {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           new CopyFiles().copyFiles();  
      }  
      public void copyFiles() {  
           String destination = "/Users/S_Das/Documents/Java/new.xml";  
           String source = "/Users/S_Das/Documents/Java/jdk-8u60-linux-i586.gz";  
           try {  
                // OPTION 01  
                Files.copy(Paths.get("/Users/S_Das/Documents/Java/pom.xml"), new FileOutputStream(new File(destination)));  
                // OPTION 02  
                Files.copy(new FileInputStream(new File(source)),  
                          Paths.get("/Users/S_Das/Documents/Java/NEW_jdk-8u60-linux-i586.gz"),  
                          StandardCopyOption.REPLACE_EXISTING);  
           } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }  

Java EPOCH to Date Time and vice verse converter







package DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateExample {

 public static void main(String[] args) {
  ChangeDate dateuse = new ChangeDate();
  dateuse.EpochToDateTime();
  dateuse.dateTimeToEpoch();

 }

}

class ChangeDate {

 String formatDate;

 public void EpochToDateTime() {
  DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
  format.setTimeZone(TimeZone.getDefault());
  formatDate = format.format(new Date().getTime());
  System.out.println(formatDate);
  System.out.println("epoch " + new Date().getTime());
  // System.out.println(System.currentTimeMillis());
 }

 public void dateTimeToEpoch() {
  // String dateTime = "20/11/2015 18:55:00";
  SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss.SSS");
  // format.setTimeZone(TimeZone.getDefault());
  try {
   Date epoch = format.parse(formatDate);
   System.out.println(epoch.getTime());

  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


Monday, October 12, 2015

Java Log Helper using Scanner

Problem Statement: 
Sometimes it becomes very tedious to find some thing in a huge log file, most of the time we look for some string pattern in the log file, For example we look for keywords like "success", "failure", "error", "warning", "java stack trace", "error code" , etc.
Through  this utility, without opening the log file and effortlessly  we can find the string we are looking for within the log file. If there is a match the utility will return a match along with the line numbers the string you are looking for is present.
From the sample.txt file I am finding the string "com0113:



OUTPUT



 package sudas.utils;  
 import java.io.BufferedInputStream;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.FileNotFoundException;  
 import java.io.IOException;  
 import java.util.Scanner;  
 public class LogScannerHelper {  
      public static void main(String[] args) {  
           // new Bufferedreader().inputStream();  
           new scanner().scanLines();  
      }  
 }  
 class scanner {  
      // read the content of the log file and search for a perticular string  
      public void scanLines() {  
           try {  
                Scanner scan = new Scanner(new File("C:/Test/sudas.txt"));  
                String match = "of";  
                int lineNumber = 1;  
                while (scan.hasNextLine()) {  
                     String line = scan.nextLine();  
                     // System.out.println(line+'\t'+lineNumber++);  
                     Scanner string = new Scanner(line);  
                     String completeString = string.findInLine("companies");  
                     {  
                          int lineNum = lineNumber++;  
                          if (completeString != null) {  
                               System.out.println(completeString + '\t' + "-- at line number "+lineNum);  
                          }  
                     }  
                }  
           } catch (FileNotFoundException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }  

Wednesday, October 7, 2015

Java scanner utility to take input from console and save inputs in a file

This utility takes keyboard input what you type in console and saves all the string in a file

This is what i am inputting in the console

And this is where it is getting saved in a file



 package Scanner;  
 import java.io.BufferedOutputStream;  
 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.Scanner;  
 public class ScannerHelper {  
      String line;  
      public static void main(String[] args) throws IOException {  
           // TODO Auto-generated method stub  
           new ScannerHelper().scanner();  
      }  
      public void scanner() throws IOException {  
           Scanner scan = new Scanner(System.in);  
           File f = new File("C:/Test/createdbyscanner.txt");  
           BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));  
           if (f.exists()) {  
                f.delete();  
           }  
           boolean condition = true;  
           ArrayList<String> arraylist = new ArrayList<String>();  
           while (condition) {  
                line = scan.nextLine();  
                arraylist.add(line);  
                if (line.equals(".")) {  
                     break;  
                }  
           }  
           Iterator<String> itr = arraylist.iterator();  
           while (itr.hasNext()) {  
                String scannerLine = itr.next().toString();  
                // System.out.println(scannerLine);  
                try {  
                     System.out.println("from scanner " + scannerLine);  
                     bos.write(scannerLine.getBytes());  
                     bos.write(' ');  
                     bos.write('\n');  
                } catch (IOException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                }  
           }  
           bos.flush();  
           bos.close();  
      }  
 }  

Monday, September 14, 2015

Java find if content of Column B is present in Column A

/* * This code is to compare content of one columns to other (For example if all elements in Column B is * present in Column A. e * BUG: this code has an issue right now. For NULL between the column cell it is * returning true */

If this is my excel sheet
The output would look like

Banana present in column A
Orange present in column A
Palm present in column A
Pinaple present in column A
Monkey not present in column A


 package jexel;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import jxl.Cell;  
 import jxl.Sheet;  
 import jxl.Workbook;  
 import jxl.read.biff.BiffException;  
 public class CompareColumns {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           CompareC C = new CompareC();  
           C.Compare1();  
      }  
 }  
 /*  
  * This is a code compare two columns between excel sheet Here I am comparing  
  * column A and column B. This is similar to index search  
  *   
  * BUG: this code has an issue right now. For NULL between the column cell it is  
  * returning true  
  */  
 class CompareC {  
      File file = new File("/Users/S_Das/Documents/Java/Test.xls");  
      String col1Content;  
      String col2Content;  
      public void Compare1() {  
           try {  
                Workbook workbook = Workbook.getWorkbook(new FileInputStream(file));  
                Sheet sheet = workbook.getSheet(0);  
                int rowNum = sheet.getRows();  
                ArrayList<String> list1 = new ArrayList<String>();  
                for (int i = 0; i < rowNum; i++) {  
                     Cell col1 = sheet.getCell(0, i);  
                     Cell col2 = sheet.getCell(1, i);  
                     col1Content = col1.getContents();  
                     col2Content = col2.getContents();  
                     list1.add(i, col1Content);  
                     // System.out.println("col2Content "+col2Content);  
                }  
                for (int i = 0; i < rowNum; i++) {  
                     Cell col1 = sheet.getCell(0, i);  
                     Cell col2 = sheet.getCell(1, i);  
                     col1Content = col1.getContents();  
                     col2Content = col2.getContents();  
                     /*  
                      * if (!(col2Content.isEmpty())) {  
                      * System.out.println(sheet.getCell(1, i).getRow() + " " +  
                      * sheet.getCell(1, i).getColumn() + " " + "empty"); }  
                      */  
                     if (list1.contains(col2Content) && (!(col2Content.isEmpty()))) {  
                          System.out.println(col2Content + " present in column A");  
                     }  
                     else if ((!(col2Content.isEmpty()))) {  
                          System.out  
                                    .println(col2Content + " not present in column A");  
                     }  
                }  
           } catch (BiffException | IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }