Operate CSV files in Java

Source: Internet
Author: User
1. Basic Introduction

{
Tagshow (event, 'csv ');
} "Href =" javascript:; "target =" _ Self ">CSVFiles in the format are often encountered. What is CSV format? CVs stands for comma-separated values, which is a typical file separated by commas (,). For example, the following file format:

Name, Company

Zhangsan, IBM

Lisi, Oracle

This is a typical CSV file. However, it can also be extended to characters separated by other symbols, such

Name # Company

Zhangsan # IBM

Lisi # Oracle

This is also in CSV format.

{
Tagshow (event, 'java ');
} "Href =" javascript:; "target =" _ Self ">JavaThe open-source framework cvsreader provides a lightweight, simple, and convenient unified operation interface available. The following describes how to operate the CVS format.

2 installation and {
Tagshow (event, '% Ca % B9 % D3 % c3 ');
} "Href =" javascript:; "target =" _ Self "> Use2.1 {
Tagshow (event, '% CF % C2 % D4 % d8 ');
} "Href =" javascript:; "target =" _ Self "> Download

Currently, the latest version of CSV reader is 1.8. We can

Http://opencsv.sourceforge.net/

Download the latest csvreader package.

2.2 Installation

You can directly store the jar package under the class path of the development project.

3. Basic Introduction to reading CSV files 3.1

First, you must create a csvreader to read a CSV file, as shown below:

Csvreader reader = new csvreader (Reader R, char C );

The first parameter is to read the file, and the second parameter is a delimiter, such as "," or "#".

In addition, there are several other parameters that can be viewed by the API, such

Csvreader reader = new csvreader (inputstream R, char C, charset );

Second, you generally need to read the header information as follows:

Reader. readheaders ();

String [] headers = reader. getheaders ();

After reading the file, the pointer is moved to the next row, that is, the object content can be read.

If there are multiple rows, you can use a loop, for example:

While (reader. readrecord ()){

For (INT I = 0; I

String value = reader. Get (headers [I]);

System. Out. Print (Value + "");

}

System. Out. println ("");

}

3.2 Comprehensive examples

Create a test file test. CVs under drive C with the following content:

A # B # C

1 #2 #3

4 #5 #6

The following is the parsing code:

Public static void main (string [] ARGs) throws exception {

Csvreader reader = new csvreader (New filereader ("C: // csv.txt "),'#');

Reader. readheaders ();

String [] headers = reader. getheaders ();

While (reader. readrecord ()){

For (INT I = 0; I

String value = reader. Get (headers [I]);

System. Out. Print (Value + "");

}

System. Out. println ("");

}

}

Run the above program and you can see the output

1 2 3

4 5 6

4. Write CSV files

Writing CSV files is also relatively simple. To write each column, you only need to call

Csvwriter. Write ()

Besides, call csvwriter. endrecord ();

End a row

After writing the file, remember to refresh it and turn it off, as shown below:

Csvwriter. Flush ();

Csvwriter. Close ();

The Code is as follows:

PublicStaticVoidMain (string [] ARGs)ThrowsException {

Csvwriter =NewCsvwriter (NewFilewriter ("C: // test. Text "),'#');

Csvwriter. Write ("name ");

Csvwriter. Write ("company ");

Csvwriter. endrecord ();

Csvwriter. Write ("11 ");

Csvwriter. Write ("12 ");

Csvwriter. endrecord ();

Csvwriter. Write ("21 ");

Csvwriter. Write ("22 ");

Csvwriter. Flush ();

Csvwriter. Close ();

}

}

Run the above program. You can see that a file is created under drive C.

Test. Text

The content is as follows:

Name # Company

11 #12

21 #22

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------

Use JSP to export CSV files

JSP is written as follows:

<% @ Page contenttype = "application/vnd. MS-CSV; charset = GBK "Language =" Java "%> <% response. setheader ("content-disposition", "attachment; filename=check_charge.csv; style = vnd. ms-csv.numberformat: @ "); % & gt; <% @ taglib prefix =" S "uri ="/Struts-tags "% & gt; <s: iterator value =" pageilchargeloglist "& gt; <s: property value = "iluser. iptvid "/>, <s: Date name =" chargetime "format =" yyyymmddhhmmss "/>, ITV Fu Cai recharge, <s: property value =" chargemoney "/>
</S: iterator>

 

 

Action return this page can be exported

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.