Saturday, 9 March 2013

Read / Write CSV file in Java

If 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 using CSVWriter 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 takes String [] 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.

http://viralpatel.net/blogs/java-read-write-csv-file/

Read More

Java CSV

image

Other Resources


Ricebridge CSV Manager allows you to read and write CSV files from within your Java application. This component covers all sorts of boundary cases and bad data that you'd rather not think about.
CSVReader and CSVWriter from Canadian Mind Products are classes to read, write, align and pack CSV (comma separated value) files.

Package com.csvreader

Class Summary

CsvReader
A stream based parser for parsing delimited text data from a file or a stream.

CsvWriter
A stream based writer for writing delimited text data to a file or a stream.

http://www.csvreader.com/java_csv.php

Read More

Sunday, 3 March 2013

Camera calibration using Open CV

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code.

calib3d module. Camera calibration and 3D reconstruction

CameraCalSqChess
Read More

Wednesday, 27 February 2013

Mini Hydraulic power pack

Model

HY 10

Mini power packs is an extremely versatile product, composed by modular elements with smaller dimension and a large range of simple assembling parts. It guarantees and permits solution of a lots of applications in compact way without any necessity of tubes and supplements assembling.
Most of them are composed of:
- Connecting central housing, predisposed for using different valves
- High performance gear pump
- D.C. or A.C. eletric motors
- Steel or plastic oil tank
- Different modular plates

http://www.hydroven.com/minicentraline-HY10-compatte.php?lang=en&s=m
Read More

Mini hydraulic Power Pack – hydraulic calculations

 

mini power pack

Mini Power Pack
This modular mini power unit forms the basis for a wide range of mobile and industrial mini hydraulic systems. They fit into our range between our micro and standard units for applications requiring up to 5.5 kW. They are a flexible small hydraulic power pack unit for AC or DC single and double-acting applications to 30 litres/minute, with tank capacities between 1 and 55 litres and pressure to 250 bar.

At the centre of these small hydraulic power packs is a high quality aluminium casting that joins the gear pump to the drive motor. The gear pump flow is delivered to a check valve, relief valve and a flexible range of directional control valves.

To drive the pumps we offer DC motors from 500W to 3kW, in 12V and 24VDC. Standard IEC frame AC motors from 0.37 to 5.5kW.

Pump sizes range from 0.25cc to 9.8cc/rev with tandem and cast iron pumps are options.

Tanks from 1 Litre to 55Litre are available, 1.5, 3, 5 and 8Litre in thermo-plastic.

Some of the 100’s of other options are:-

Hand Pumps

Cetop03 Valves

Solenoid Lowering Valves

Manual Lowering Valves

Pressure compensated Flow insert

Typical applications include :-

Plant trailers, the 3kW 24V motor is commonly used for the ramps of plant trailers.

Portable power pack, for remote these small hydraulic power packs are light and compact.

Step lifts, the small hydraulic pumps and 500Watt DC Motors are used for Step lifts.

Hydraproducts offers its worldwide OEMs and distributors reliable products through design and manufacturing expertise. The quality of in-house design, manufacturing, experience in small hydraulic power units ensures a match to your requirements.

http://www.hydraproducts.co.uk/hydraulic-calculators/pump-flow.aspx

Read More

Eudoxus - 3D Vision - Cognitive Vision

 

EUDOXUS

Eudoxus is a 3D vision sensor integrating the ASUS® Xtion PRO LIVE RGB and Depth Sensor with a Gumstix® OveroSTORM COM embedded Linux system. It is capable of streaming depth and RGB data in real time to either a file or live video viewer on a host machine over Wifi or Ethernet. Eudoxus is a stand-alone 3D vision sensor for mobile mapping applications.

Eudoxus’ name is derived from the Greek navigator Eudoxus of Cyzicus, who was a pioneer of navigation and exploration in 130 BCE.

APPLICATIONS

Eudoxus' software and data sets are based around open interfaces, standards, and formats, which makes it an ideal sensor for developing 3D vision sensing applications. Because the platform can stream RGB images, depth, and audio data, it can be used to develop other multimedia applications as well. Eudoxus V0.7 is a research platform and is currently available only to researchers, developers, and academic institutions at limited quantities. Release V1.0, coming in 2013Q1, will be available for general release.

 

http://www.roadnarrows-store.com/products/sense/vision/cognitive-vision/eudoxus-3d-vision.html

Read More

Capella - Stereo Vision Camera Reference Design

 

capella-Stereo vision Camera Reference Design

Capella is the world’s first embedded pixel synchronous Stereo Vision Camera Reference Design for Texas Instruments’ (TI) OMAP35x and AM/DM37x processor on Gumstix® Overo® COMs, designed and developed by e-con Systems.

Capella - Stereo vision camera reference design

Click to view larger image

Overview of Capella + Depth Measurement Demo

The Stereo vision Camera Reference Design, Capella, can be used by customers to develop their Stereo Vision Camera algorithms and also by customers who would want to integrate Stereo Vision Camera in their product design. The Capella features the Gumstix® Overo® COM, Tobi base board and a camera daughter card e-CAM_9V024_STEREO. The e-CAM_9V024_STEREO delivers pixel-synchronous stereo frames to the OMAP/DM37x processor.

Capella – Stereo Vision Camera reference design features
Download Capella Documents

Stereo vision camera board – e-CAM_9V024_STEREO

Pixel synchronous Dual camera streams at 30fps

Connects directly to 27pin camera FPC connector of Gumstix®

Overo® COM

Directly fits over Gumstix Tobi Base board

Adjustable baseline on customer request

Factory calibrated S-mount lens pair

Standard V4L2 Compliant driver with associated APIs and

custom IOCTLs

Sample V4L2 applications demonstrating the synchronous

stereo operation

Complete Stereo Vision Camera development packag

http://www.e-consystems.com/stereo-vision-camera.asp

Read More

Tuesday, 26 February 2013

DC Thyristor Controller

 

Become Our Sales Partner

5. DC thyristor controllers

  • half-controlled symmetrical bridge circuit with excitation voltage source
  • adjustable current controller and thus very suitable for permanently excited motors
  • for motors with external and permanent excitation
  • one-quadrant operation
  • control range with armature voltage control 1:30
  • control range with tachometer control 1:100
  • degree of protection IP 00

 

http://www.peter-electronic.com/en/contact/sales-network/become-our-sales-partner.html

Read More

photogrammetry software – measurements from image

 

Xsigno is an entry solution to 3D coordinate measurement for architecture, interior design and planning. We provide a close-range photogrammetry software for the measurement, visualization and documentation using digital photography.

http://www.xsigno.com/index.html

Read More

G570Z – Industrial Rugged PC

As Winmate is going to launch a new series of ruggedized handheld device in 2011, we want to first introduce you our 5.7" handheld device - G570Z. Its compact design with PC based platform allows users to carry around easily while working both inside and outside of their office. G570Z possesses strong mobility including 3G, GPS and Wi-Fi, making remote office work a lot easier.

http://www.winmate.com.tw/TabletPC/rugged_tablet_5.7handheld.htm

Read More