java4all@1986 java. Powered by Blogger.

How to read data into csv file?

>> Tuesday, May 31, 2011

For example we are having an excel sheet like below
File Format:
ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued
1,Chai,1,1,10 boxes x 20 bags,18,39,0,10,FALSE
2,Chang,1,1,24 - 12 oz bottles,19,17,40,25,FALSE

To read Data from csv file we will use CsvReader class.

import java.io.FileNotFoundException;
import java.io.IOException;

import com.csvreader.CsvReader;

public class CsvReaderExample {

 public static void main(String[] args) {
  try {
   CsvReader products = new CsvReader("products.csv");
  
   products.readHeaders();//This method is used to headers from csv file

   while (products.readRecord()) //This method is used to read data from records
   {
    String productID = products.get("ProductID");
    String productName = products.get("ProductName");
    String supplierID = products.get("SupplierID");
    String categoryID = products.get("CategoryID");
    String quantityPerUnit = products.get("QuantityPerUnit");
    String unitPrice = products.get("UnitPrice");
    String unitsInStock = products.get("UnitsInStock");
    String unitsOnOrder = products.get("UnitsOnOrder");
    String reorderLevel = products.get("ReorderLevel");
    String discontinued = products.get("Discontinued");
    
    // perform program logic here
    System.out.println(productID + ":" + productName);
   }
 
   products.close();
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

}


This is the simple way to read data from csv file.

Read more...

How to create a directory and a floder inside the directory and how to copy the files from source to target directory?

For example we had a floder D;//drafts where there are csv files. and a floder D://pdffiles it consists of some pdf files.

The first challenge is to create a directory like D://archieve/currentmonthandyear/drafts/csv files.
the second challenge is to create a directory like D://archieve/currentmonthandyear/drafts/pdf files.

For this we need a property files assume me.properties
 property file consists of key value pairs as shown below

savefile=D:/Drafts/
pdffiles=D:/Payslips/
archivefiles=D:/archive/
archivedrafts=/Drafts/
archivepdffiles=/Payslips/
csvfile=D:/Drafts/payslip.csv

We need a class to call kets from property files ,Take user defined classes  and write the following code,
JPropertyReader .java
import java.util.ResourceBundle;
public class JPropertyReader {
 public static String readProperty(String key) {
  String PROPERTY_FILE=MessageConstants.prop_dir;
  ResourceBundle rb = ResourceBundle.getBundle(PROPERTY_FILE);
  String propvalue=rb.getString(key);
  return propvalue;
 }
 public static String  readlog4JProperty(String strKey) 
 { String PROPERTY_NAME="ATR";
  ResourceBundle rb=ResourceBundle.getBundle(PROPERTY_NAME);
  String strPropertyValue=rb.getString(strKey);
  return strPropertyValue;
 }
}



ResourcebBundle .java is an abstarct class and it is available in java.utill.* package

Take a class to create dirctory and store files in it and delete files.

package com.centris.atr.common.util;

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

import org.apache.commons.io.FileUtils;

public class FileOperations {
public void copy_Directory_Files(){
 System.out.println("----u r in copy_directory==========");
 String[] monthName = {"January", "February",
   "March", "April", "May", "June", "July",
   "August", "September", "October", "November",
 "December"};
 Calendar cal = Calendar.getInstance(); 
 String monthSalary = monthName[(cal.get(Calendar.MONTH)-1)]; //current month-1 means pervious month
 int yearSalary = cal.get(Calendar.YEAR);//current year
 String savefile=JPropertyReader.readProperty("savefile"); D://drafts
 File srcDir = new File(savefile);
 File srcDir1 = new File(JPropertyReader.readProperty("pdffiles"));//D://pdffiles
 String archivedraftfiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivedrafts");//here we will create directory like D://archieve/perviousmonth with year/drafts
 String archievepdffiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivepdffiles");  //D://archieve/perviousmonth with year/pdffiles
 File trgDir = new File(archivedraftfiles);
 File trgDir1 = new File(archievepdffiles);
 //boolean success=true;
 try {
   FileUtils.copyDirectory(srcDir, trgDir);//copying files from source to target
   FileUtils.copyDirectory(srcDir1, trgDir1);
  } catch (IOException e) {
  e.printStackTrace();
 }
  
}
public static boolean delete_Files(){
 boolean t=true;
 String savefile=JPropertyReader.readProperty("savefile");
 String pdffiles=JPropertyReader.readProperty("pdffiles");
 File directory = new File(savefile);
 File[] files = directory.listFiles();//deleting the files csvfile from d://drafts/csvfile
 for (File file : files) {
  if (!file.delete())
  {
   t=false;
  }
 }
 File directory1 = new File(pdffiles);
 File[] files1 = directory1.listFiles();
 for (File file : files1) {
  if (!file.delete())
  {
   t=false;
  }
 }
 return t;
}
public static void main(String args[]){
FileOperations objFileOperations=new FileOperations();
  objFileOperations.copy_Directory_Files();
 
}

Read more...

What is difference between URI,URL,URN?

URI: It is a String specification which can be used to refer an object from server.
URL:It is a String specification ,which can be used to refer an object from server through it's locator.
URN: It is a String specification which can be used to refer an object though it's logical name.

Read more...

Diffrence between Web Server and Application Server?

(1) Webserver serves pages for viewing in web browser, application server provides exposes businness logic for client applications through various protocols

(2) Webserver exclusively handles http requests.application server serves bussiness logic to application programs through any number of protocols.

(3) Webserver delegation model is fairly simple,when the request comes into the webserver,it simply passes the request to the program best able to handle it(Server side program). It may not support transactions and database connection pooling.

(4) Application server is more capable of dynamic behaviour than webserver. We can also configure application server to work as a webserver.Simply applic! ation server is a superset of webserver.

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP