Search This Site
Jul 09 2008 03:50 UTC | ||||||||||
XML ManagerStandard Edition |
• Try the Demo! • Download the FREE Trial! • Product Home Page |
• Getting Started Guide • How You Can Save Money • All Your Purchase Options |
Learn how to load and save CSV files:
String[] arraysString[] arrays to a CSV fileTo compile and run the Java programs in this example, either import them into your IDE
(say, Eclipse or NetBeans), or follow
the standard manual compilation instructions.
When running the programs, run them in the same folder as this README.htm file, so that
they can find their input files.
First, let's look at loading CSV data. Here's the CSV file we want to load:
cities.csvName, Country Boston, USA Paris, France London, England Tokyo, Japan
CSV Manager can load this data into a list of two element String[] arrays.
The first element contains the city name, and the second element contains the country name.
The easiest way to use CSV Manager is use the load method. Here it is:
CsvManager csvman = new CsvManager(); List data = csvman.load( "cities.csv" );
What's happening here? First we create a new CsvManager object. Then we call the load method, passing in the
file path to the cities.csv file as a String (You can also use a File object, if you like).
We get back a List of String[] arrays. Let's loop through this list and have a look at the data.
System.out.println( "Name\tCountry" );
for( int line = 1; line < data.size(); line++ ) {
String[] fields = (String[]) data.get(line);
System.out.println( fields[0]+"\t"+fields[1] );
}
We're going to print out the name and country of each city. We use a for loop to run through all the
data lines we found. For each data line, we get the data fields String[] array, using the List.get method
and then we print out the two data fields using field[0] for the city name and field[1] for the country.
Here's the full source code. You can cut-and-paste this into your own project to get something working right away.
Load.java
/** You may copy this example and use it for any purpose, commercial or otherwise. */
import com.ricebridge.csvman.CsvManager;
import java.util.*;
import java.io.*;
/** Load some CSV data. */
public class Load {
public static void main( String[] args ) throws Exception {
// load the data
CsvManager csvman = new CsvManager();
List data = csvman.load( "cities.csv" );
// print the data
System.out.println( "Name\tCountry" );
for( int line = 1; line < data.size(); line++ ) {
String[] fields = (String[]) data.get(line);
System.out.println( fields[0]+"\t"+fields[1] );
}
}
}
Ok. What about saving CSV data? As an example, let's say we want to create a CSV file listing all the numbers from 1 to 10, with their squares. Here's what we want:
Integer,Square 1,1 2,4 3,9 4,16 5,25 6,36 7,49 8,64 9,81 10,100
We'll create this data by looping over the numbers from 1 to 10, and multiplying each number by itself. We'll store the
each pair of numbers as Strings in a String[] array.
And we'll put all the String[] arrays into an ArrayList (a type of List).
Here's the code to do that:
List data = new ArrayList();
data.add( new String[] {"Number", "Square"} );
for( int line = 1; line <= 10; line++ ) {
String[] fields
= new String[] { ""+line, ""+(line*line) };
data.add( fields );
}
To convert the line variable into a String we append to an empty String.
Thus the integer line becomes the String ""+line. Also note that the first line of data is
actually the CSV file headers: Number, Square.
Now we pass the data variable to the save method, and CSV Manager will save it as a CSV file.
Here's how:
CsvManager csvman = new CsvManager(); csvman.save( "numbers.csv", data );
This bit of code just tells CSV Manager to create a CSV file called numbers.csv,
and to save all the String[] arrays in the
data List to this file.
Here's the full source code:.
Save.java
/** You may copy this example and use it for any purpose, commercial or otherwise. */
import com.ricebridge.csvman.CsvManager;
import java.util.*;
import java.io.*;
/** Save some CSV data. */
public class Save {
public static void main( String[] args ) throws Exception {
// create some test data to save
List data = new ArrayList();
data.add( new String[] {"Number", "Square"} );
for( int line = 1; line <= 10; line++ ) {
String[] fields
= new String[] { ""+line, ""+(line*line) };
data.add( fields );
}
// save the data
CsvManager csvman = new CsvManager();
csvman.save( "numbers.csv", data );
}
}
Here is a list of all the files used in this example. Note that the actual source code is slightly longer than the examples above, which have been abridged for clarity.
cities.csv filenumbers.csvFeel free to experiment with these files and see what happens. You can also use them as the basis for your own solutions.
Please feel free to email us at examples@ricebridge.com if you have any questions or comments about this example.
Got a question for us?
Just Ask!
$15 Gift Certificate for every bug you find.