java4all@1986 java. Powered by Blogger.
Showing posts with label csv file. Show all posts
Showing posts with label csv file. Show all posts

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...

How to write read into CSV File?

>> Thursday, May 26, 2011

For example if we want create a csv file like
          Name   Place  Age
         bhas      VMR   23
         Aja        ONG   21





Here below code will explain easily how to to write the above data into CSV File
 
public class GenerateCsv1
{
    String name="bhaskar";
   

   private static void generateCsvFile(String sFileName)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName);

      
    writer.append("Name");
        writer.append(',');
writer.append("Place");
        writer.append(',');
        writer.append("Age");
        writer.append('\n');

        writer.append("Bhas");
        writer.append(',');
writer.append("Vmr");
        writer.append(',');
        writer.append("23");
            writer.append('\n');

        writer.append(""Aja);
        writer.append(',');
writer.append(""Ong);
        writer.append(',');
        writer.append("21");
        writer.append('\n');

        //generate whatever data you want

        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    }
    }
   public static void main(String [] args)
   { String csv="c://bhaskar.csv";
       GenerateCsv1.generateCsvFile(csv);
   }
}

      

Read more...

how to upload a csv file data into database

>> Tuesday, May 3, 2011

upload file link
http://javabynataraj.blogspot.com/2011/04/upload-csv-file-into-mysql-database.html

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