CSV file Introduction
The comma-separated value format (csv) is a plain text format used to store data. In CSV, data fields are separated by commas (,). The program re-creates the correct field by reading the file, by starting a new piece of data each time a comma is encountered.
A csv file is a computer data file used to execute trials and a list of real organization tools separated by commas. It is often used to move two different computer programs between table data, such as relational database programs and spreadsheet programs.
The following CSV file is used as an example to describe how to implement CSV read/write operations in Java.
Writers.csv
Zhang Yimou, 1951,1114
Shenyang, 1981,0507
Sun honglei, 1970, 0816
Yan Ni, 1971,0310
CSV read operation
CSV file reading mainly uses the java. Io. bufferedreader class and Java. util. stringtokenizer class. bufferedreader is used to read files and stringtokenizer is used to divide a row of data into multiple fields.
The following is a class used to read CSV file content:
Package tip;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. filenotfoundexception;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. util. stringtokenizer;
Public class readcsv {
Public static void main (string [] ARGs ){
Try {
File CSV = new file ("C: // writers.csv"); // CSV file
Bufferedreader BR = new bufferedreader (New filereader (CSV ));
// Read until the last row
String line = "";
While (line = Br. Readline ())! = NULL ){
// Split a row of data into multiple fields
Stringtokenizer ST = new stringtokenizer (line ,",");
While (St. hasmoretokens ()){
// Multiple fields in each row are separated by tabs.
System. Out. Print (St. nexttoken () + "/t ");
}
System. Out. println ();
}
BR. Close ();
} Catch (filenotfoundexception e ){
// Capture the exception when the file object is generated
E. printstacktrace ();
} Catch (ioexception e ){
// Catch the exception when the bufferedreader object is closed
E. printstacktrace ();
}
}
}
The execution result is as follows:
CSV write operation
CSV file reading mainly uses the java. Io. bufferedwriter class.
The following is a class for writing CSV files:
Package tip;
Import java. Io. bufferedwriter;
Import java. Io. file;
Import java. Io. filenotfoundexception;
Import java. Io. filewriter;
Import java. Io. ioexception;
Public class writecsv {
Public static void main (string [] ARGs ){
Try {
File CSV = new file ("C: // writers.csv"); // CSV file
// Tracing Mode
Bufferedwriter BW = new bufferedwriter (New filewriter (CSV, true ));
// Add a new row of data
Bw. newline ();
Bw. Write ("Three shots surprised" + "," + "2009" + "," + "1212 ");
Bw. Close ();
} Catch (filenotfoundexception e ){
// Capture the exception when the file object is generated
E. printstacktrace ();
} Catch (ioexception e ){
// Catch the exception when the bufferedwriter object is closed
E. printstacktrace ();
}
}
}
The writers.csv file behind the execution is as follows:
Zhang Yimou, 1951,1114
Shenyang, 1981,0507
Sun honglei, 1970, 0816
Yan Ni, 1971,0310
Surprise With three shots
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/madai/archive/2009/12/15/5014186.aspx