Thursday, November 8, 2012

Decompress the zip file in java



import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* This utility decompresses a standard zip file to a destination directory.
* @author Ha Minh Nam
*
*/
public class UnzipUtil {
    /**
     * A constants for buffer size used to read/write data
     */
    private static final int BUFFER_SIZE = 4096;
   
    /**
     *
     * @param zipFilePath Path of the zip file to be extracted
     * @param destDirectory Path of the destination directory
     * @throws IOException IOException if any I/O error occurred
     */
    public void decompressFile(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
       
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
       
        ZipEntry entry = zipIn.getNextEntry();
       
        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                extractFile(zipIn, filePath);
            } else {
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }
   
    /**
     * Extracts a single file
     * @param zipIn the ZipInputStream
     * @param filePath Path of the destination file
     * @throws IOException if any I/O error occurred
     */
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read = 0;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }  
        bos.close();      
    }
   
    public static void main(String args[]) throws IOException
    {
     String  zipFilePath = "C:\\Users\\Home\\Downloads\\zip\\BasicStrust2ProjectEclipse.zip";
          String destFilePath = "C:\\Users\\Home\\Downloads\\zip\\output";
          UnzipUtil unzipper = new UnzipUtil();
          unzipper.decompressFile(zipFilePath, destFilePath);
    }
}

File Listener or Folder LIstener


Find out any file Created to  specific folder then go to the FileListener
interface:
----------




import java.io.File;
public interface FileListener
{
void fileChanged (File file);
}
class:
-----
import java.util.*;
import java.io.File;
import java.lang.ref.WeakReference;
/**
 * Class for monitoring changes in disk files.
 * Usage:
 *
 *    1. Implement the FileListener interface.
 *    2. Create a FileMonitor instance.
 *    3. Add the file(s)/directory(ies) to listen for.
 *
 * fileChanged() will be called when a monitored file is created,
 * deleted or its modified time changes.
 *
 * @author <a href="mailto:info@geosoft.no">GeoSoft</a>
 */
public class FileMonitor
{
  private Timer       timer_;
  private HashMap     files_;       // File -> Long
  private Collection  listeners_;   // of WeakReference(FileListener)

  /**
   * Create a file monitor instance with specified polling interval.
   *
   * @param pollingInterval  Polling interval in milli seconds.
   */
  public FileMonitor (long pollingInterval)
  {
    files_     = new HashMap();
    listeners_ = new ArrayList();
    timer_ = new Timer (true);
    timer_.schedule (new FileMonitorNotifier(), 0, pollingInterval);
  }
  /**
   * Stop the file monitor polling.
   */
  public void stop()
  {
    timer_.cancel();
  }
  /**
   * Add file to listen for. File may be any java.io.File (including a
   * directory) and may well be a non-existing file in the case where the
   * creating of the file is to be trepped.
   * <p>
   * More than one file can be listened for. When the specified file is
   * created, modified or deleted, listeners are notified.
   *
   * @param file  File to listen for.
   */
  public void addFile (File file)
  {
    if (!files_.containsKey (file)) {
      long modifiedTime = file.exists() ? file.lastModified() : -1;
      files_.put (file, new Long (modifiedTime));
    }
  }
  /**
   * Remove specified file for listening.
   *
   * @param file  File to remove.
   */
  public void removeFile (File file)
  {
    files_.remove (file);
  }
  /**
   * Add listener to this file monitor.
   *
   * @param fileListener  Listener to add.
   */
  public void addListener (FileListener fileListener)
  {
    // Don't add if its already there
    for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
      WeakReference reference = (WeakReference) i.next();
      FileListener listener = (FileListener) reference.get();
      if (listener == fileListener)
        return;
    }
    // Use WeakReference to avoid memory leak if this becomes the
    // sole reference to the object.
    listeners_.add (new WeakReference (fileListener));
  }
  /**
   * Remove listener from this file monitor.
   *
   * @param fileListener  Listener to remove.
   */
  public void removeListener (FileListener fileListener)
  {
    for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
      WeakReference reference = (WeakReference) i.next();
      FileListener listener = (FileListener) reference.get();
      if (listener == fileListener) {
        i.remove();
        break;
      }
    }
  }
  /**
   * This is the timer thread which is executed every n milliseconds
   * according to the setting of the file monitor. It investigates the
   * file in question and notify listeners if changed.
   */
  private class FileMonitorNotifier extends TimerTask
  {
    public void run()
    {
      // Loop over the registered files and see which have changed.
      // Use a copy of the list in case listener wants to alter the
      // list within its fileChanged method.
      Collection files = new ArrayList (files_.keySet());
   
      for (Iterator i = files.iterator(); i.hasNext(); ) {
        File file = (File) i.next();
        long lastModifiedTime = ((Long) files_.get (file)).longValue();
        long newModifiedTime  = file.exists() ? file.lastModified() : -1;
        // Chek if file has changed
        if (newModifiedTime != lastModifiedTime) {
          // Register new modified time
          files_.put (file, new Long (newModifiedTime));
          // Notify listeners
          for (Iterator j = listeners_.iterator(); j.hasNext(); ) {
            WeakReference reference = (WeakReference) j.next();
            FileListener listener = (FileListener) reference.get();
            // Remove from list if the back-end object has been GC'd
            if (listener == null)
              j.remove();
            else
              listener.fileChanged (file);
          }
        }
      }
    }
  }
  /**
   * Test this class.
   *
   * @param args  Not used.
   */
  public static void main (String args[])
  {
    // Create the monitor
    FileMonitor monitor = new FileMonitor (1000);
    // Add some files to listen for

    monitor.addFile (new File ("C:/Users/Home/Desktop/listerners/"));  
    // Add a dummy listener
    monitor.addListener (monitor.new TestListener());
    // Avoid program exit
    while (!false) ;
  }
  private class TestListener
    implements FileListener
  {
    public void fileChanged (File file)
    {
      System.out.println ("File changed: " + file);
    }
  }
}



Wednesday, November 7, 2012

Get the File Extension using javascript


var file1 ="50.xsl";
var file2 =30.doc";"

 var fileex=file1.split('.').pop();


Here we will get FileExtension is .xsl

Friday, November 2, 2012

BigScreen : JavaScript Full Screen API

How to Load CSV file into Database,Mapping CSV with Java beans


Java: How to Load CSV file into Database


csv-java-load
Loading CSV file into Database can be cumbersome task if your Database provider does not offer an out of box feature for this. Most of the time you’ll spend up in creating valid insert statements and putting up values escaping all special characters. Importing CSV files gets a bit complicated when you start doing things like importing files with description fields that can contain punctuation (such as commas or single-double quotation marks).
So here’s a simple Java Utility class that can be used to load CSV file into Database. Note how we used some of the best practices for loading data. The CSV file is parsed line by line and SQL insert query is created. The values in query are binded and query is added to SQL batch. Each batch is executed when a limit is reached (in this case 1000 queries per batch).

Import CSV into Database example

Let’s us check an example. Below is the sample CSV file that I want to upload in database table Customer.
employee.csv – Sample CSV file:
EMPLOYEE_ID,FIRSTNAME,LASTNAME,BIRTHDATE,SALARY
1,Dean,Winchester,27.03.1975,60000
2,John,Winchester,01.05.1960,120000
3,Sam,Winchester,04.01.1980,56000
The Table customer contains few fields. We added fields of different types like VARCHAR, DATE, NUMBER to check our load method works properly.
Table: Customer – Database table
CREATE TABLE Customer (
  EMPLOYEE_ID  NUMBER,
  FIRSTNAME    VARCHAR2(50 BYTE),
  LASTNAME     VARCHAR2(50 BYTE),
  BIRTHDATE    DATE,
  SALARY       NUMBER
)
Following is a sample Java class that will use CSVLoader utility class (we will come to this shortly).
Main.java – Load sample.csv to database
package net.viralpatel.java;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {

 private static String JDBC_CONNECTION_URL = 
   "jdbc:oracle:thin:SCOTT/TIGER@localhost:1500:MyDB";

 
 public static void main(String[] args) {
  try {

   CSVLoader loader = new CSVLoader(getCon());
   
   loader.loadCSV("C:\\employee.sql", "CUSTOMER", true);
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private static Connection getCon() {
  Connection connection = null;
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   connection = DriverManager.getConnection(JDBC_CONNECTION_URL);

  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }

  return connection;
 }
}
In above Main class, we created an object of class CSVLoader using parameterized constructor and passedjava.sql.Connection object.
Then we called the loadCSV method with three arguments. First the path of CSV file, second the table name where data needs to be loaded and third boolean parameter which decides whether table has to be truncated before inserting new records.
Execute this Java class and you’ll see the records getting inserted in table.
csv-load-java-database-example
The CSV is successfully loaded in database.
Let’s check the Utility class now. I strongly recommend you to go through below tutorials as the Utility class combines the idea from these tutorials.
  1. Batch Insert In Java – JDBC
  2. Read / Write CSV file in Java
  3. Check if String is valid Date in Java
The utility class uses OpenCSV library to load and parse CSV file. Then it uses the idea of Batching in JDBC to batch insert queries and execute them. Each CSV value is checked if it is valid date before inserting.
CSVLoader.java – Utility class to load CSV into Database
package net.viralpatel.java;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import au.com.bytecode.opencsv.CSVReader;

/**
 * 
 * @author viralpatel.net
 * 
 */
public class CSVLoader {

 private static final 
  String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
 private static final String TABLE_REGEX = "\\$\\{table\\}";
 private static final String KEYS_REGEX = "\\$\\{keys\\}";
 private static final String VALUES_REGEX = "\\$\\{values\\}";

 private Connection connection;
 private char seprator;

 /**
  * Public constructor to build CSVLoader object with
  * Connection details. The connection is closed on success
  * or failure.
  * @param connection
  */
 public CSVLoader(Connection connection) {
  this.connection = connection;
  //Set default separator
  this.seprator = ',';
 }
 
 /**
  * Parse CSV file using OpenCSV library and load in 
  * given database table. 
  * @param csvFile Input CSV file
  * @param tableName Database table name to import data
  * @param truncateBeforeLoad Truncate the table before inserting 
  *    new records.
  * @throws Exception
  */
 public void loadCSV(String csvFile, String tableName,
   boolean truncateBeforeLoad) throws Exception {

  CSVReader csvReader = null;
  if(null == this.connection) {
   throw new Exception("Not a valid connection.");
  }
  try {
   
   csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

  } catch (Exception e) {
   e.printStackTrace();
   throw new Exception("Error occured while executing file. "
     + e.getMessage());
  }

  String[] headerRow = csvReader.readNext();

  if (null == headerRow) {
   throw new FileNotFoundException(
     "No columns defined in given CSV file." +
     "Please check the CSV file format.");
  }

  String questionmarks = StringUtils.repeat("?,", headerRow.length);
  questionmarks = (String) questionmarks.subSequence(0, questionmarks
    .length() - 1);

  String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
  query = query
    .replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
  query = query.replaceFirst(VALUES_REGEX, questionmarks);

  System.out.println("Query: " + query);

  String[] nextLine;
  Connection con = null;
  PreparedStatement ps = null;
  try {
   con = this.connection;
   con.setAutoCommit(false);
   ps = con.prepareStatement(query);

   if(truncateBeforeLoad) {
    //delete data from table before loading csv
    con.createStatement().execute("DELETE FROM " + tableName);
   }

   final int batchSize = 1000;
   int count = 0;
   Date date = null;
   while ((nextLine = csvReader.readNext()) != null) {

    if (null != nextLine) {
     int index = 1;
     for (String string : nextLine) {
      date = DateUtil.convertToDate(string);
      if (null != date) {
       ps.setDate(index++, new java.sql.Date(date
         .getTime()));
      } else {
       ps.setString(index++, string);
      }
     }
     ps.addBatch();
    }
    if (++count % batchSize == 0) {
     ps.executeBatch();
    }
   }
   ps.executeBatch(); // insert remaining records
   con.commit();
  } catch (Exception e) {
   con.rollback();
   e.printStackTrace();
   throw new Exception(
     "Error occured while loading data from file to database."
       + e.getMessage());
  } finally {
   if (null != ps)
    ps.close();
   if (null != con)
    con.close();

   csvReader.close();
  }
 }

 public char getSeprator() {
  return seprator;
 }

 public void setSeprator(char seprator) {
  this.seprator = seprator;
 }

}
The class looks complicated but it is simple :)
The loadCSV methods combines the idea from above three tutorials and create insert queries.
Following is the usage of this class if you want to use it in your project:
Usage
CSVLoader loader = new CSVLoader(connection);
loader.loadCSV("C:\\employee.sql", "TABLE_NAME", true);
Load file with semicolon as delimeter:
CSVLoader loader = new CSVLoader(connection);
loader.setSeparator(';');
loader.loadCSV("C:\\employee.sql", "TABLE_NAME", true);
Load file without truncating the table:
CSVLoader loader = new CSVLoader(connection);
loader.loadCSV("C:\\employee.sql", "TABLE_NAME", false);
Hope this helps.

Download Source Code

Load_CSV_Database_Java_example.zip (2.05 MB)

Related Posts

    
ShareKeep unread

    Read / Write CSV file in Java


    random-textIf you want to work with Comma-separated Files (CSV) in Java, here’s a quick API for you.
    As Java doesn’t support parsing of CSV files natively, we have to rely on third party library. Opencsv is one of the best library available for this purpose. It’s open source and is shipped with Apache 2.0 licence which makes it possible for commercial use.
    Let’s us see different APIs to parse CSV file. Before that we will need certain tools for this example:
    Tools & Technologies
    1. Java JDK 1.5 or above
    2. OpenCSV library v1.8 or above (download)
    3. Eclipse 3.2 above (optional)
    Let’s get started.

    1. Reading CSV file in Java

    We will use following CSV sample file for this example:
    File: sample.csv
    COUNTRY,CAPITAL,POPULATION
    India,New Delhi, 1.21B
    People's republic of China,Beijing, 1.34B
    United States,Washington D.C., 0.31B
    
    Read CSV file line by line:
    String csvFilename = "C:\\sample.csv";
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
    String[] row = null;
    while((row = csvReader.readNext()) != null) {
     System.out.println(row[0]
               + " # " + row[1]
               + " #  " + row[2]);
    }
    //...
    csvReader.close();
    
    In above code snippet, we use readNext() method of CSVReader class to read CSV file line by line. It returns a String array for each value in row.
    It is also possible to read full CSV file once. The readAll() method of CSVReader class comes handy for this.
    String[] row = null;
    String csvFilename = "C:\\work\\sample.csv";
    
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
    List content = csvReader.readAll();
    
    for (Object object : content) {
     row = (String[]) object;
     
     System.out.println(row[0]
                + " # " + row[1]
                + " #  " + row[2]);
    }
    //...
    csvReader.close();
    
    The readAll() method returns a List of String[] for given CSV file.
    Both of the above code snippet prints output:
    Output
    COUNTRY # CAPITAL #  POPULATION
    India # New Delhi #   1.21B
    People's republic of China # Beijing #   1.34B
    United States # Washington D.C. #   0.31B
    
    Use different separator and quote characters
    If you want to parse a file with other delimiter like semicolon (;) or hash (#), you can do so by calling a different constructor of CSVReader class:
    CSVReader reader = new CSVReader(new FileReader(file), ';')
    //or
    CSVReader reader = new CSVReader(new FileReader(file), '#')
    
    Also if your CSV file’s value is quoted with single quote (‘) instead of default double quote (“), then you can specify it in constructor:
    CSVReader reader = new CSVReader(new FileReader(file), ',', '\'')
    
    Also it is possible to skip certain lines from the top of CSV while parsing. You can provide how many lines to skip in CSVReader’s constructor. For example the below reader will skip 5 lines from top of CSV and starts processing at line 6.
    CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 5);
    

    2. Writing CSV file in Java

    Creating a CSV file is as simple as reading one. All you have to do is it create the data list and write usingCSVWriter class.
    Below is the code snippet where we write one line in CSV file.
    String csv = "C:\\output.csv";
    CSVWriter writer = new CSVWriter(new FileWriter(csv));
    
    String [] country = "India#China#United States".split("#");
    
    writer.writeNext(country);
    
    writer.close();
    
    We created object of class CSVWriter and called its writeNext() method. The writeNext() methods takesString [] as argument.
    You can also write a List of String[] to CSV directly. Following is code snippet for that.
    String csv = "C:\\output2.csv";
    CSVWriter writer = new CSVWriter(new FileWriter(csv));
    
    List<String[]> data = new ArrayList<String[]>();
    data.add(new String[] {"India", "New Delhi"});
    data.add(new String[] {"United States", "Washington D.C"});
    data.add(new String[] {"Germany", "Berlin"});
    
    writer.writeAll(data);
    
    writer.close();
    
    We used writeAll() method of class CSVWriter to write a List of String[] as CSV file.

    3. Mapping CSV with Java beans

    In above examples we saw how to parse CSV file and read the data in it. We retrieved the data as String array. Each record got mapped to String.
    It is possible to map the result to a Java bean object. For example we created a Java bean to store Country information.
    Country.java – The bean object to store Countries information.
    package net.viralpatel.java;
    
    public class Country {
     private String countryName;
     private String capital;
    
     public String getCountryName() {
      return countryName;
     }
    
     public void setCountryName(String countryName) {
      this.countryName = countryName;
     }
    
     public String getCapital() {
      return capital;
     }
    
     public void setCapital(String capital) {
      this.capital = capital;
     }
    }
    
    Now we can map this bean with Opencsv and read the CSV file. Check out below example:
    ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
    strat.setType(Country.class);
    String[] columns = new String[] {"countryName", "capital"}; // the fields to bind do in your JavaBean
    strat.setColumnMapping(columns);
    
    CsvToBean csv = new CsvToBean();
    
    String csvFilename = "C:\\sample.csv";
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
    
    List list = csv.parse(strat, csvReader);
    for (Object object : list) {
     Country country = (Country) object;
     System.out.println(country.getCapital());
    }
    
    Check how we mapped Country class using ColumnPositionMappingStrategy. Also the methodsetColumnMapping is used to map individual property of Java bean to the CSV position. In this example we map first CSV value to countryName attribute and next to capital.

    4. Dumping SQL Table as CSV

    OpenCSV also provides support to dump data from SQL table directly to CSV. For this we need ResultSet object. Following API can be used to write data to CSV from ResultSet.
    java.sql.ResultSet myResultSet = getResultSetFromSomewhere();
    writer.writeAll(myResultSet, includeHeaders);
    
    The writeAll(ResultSet, boolean) method is utilized for this. The first argument is the ResultSet which you want to write to CSV file. And the second argument is boolean which represents whether you want to write header columns (table column names) to file or not.

    Download Source Code

    ReadWrite_CSV_Java_example.zip (356 KB)

    Related Posts