On Java read CSV practice

Source: Internet
Author: User

Look at the previous processing in the project is directly using the Java IO Class library to read the CSV file, the actual processing found that the CSV file itself contains a variety of special characters processing information.

The most common examples are:

1. Double quotation marks for string data containing special characters

2. Double quotation marks before a single double quote in the data

Other...

So the strings read in Java IO are all processed strings, and in some scenarios they do not meet the expected requirements. For example, what I need is the original content that does not do any processing.

Another common file format in the project, Excel used a POI to handle, but the POI does not support CSV format, then found the javacsv.

The code is simple:

Java code

Public List importcsv (String file) {

List List = new ArrayList ();

Csvreader reader = null;

try {

Initialize Csvreader and specify column delimiter and character encoding

reader = new Csvreader (file, ', ', Charset.forname ("GBK"));

while (Reader.readrecord ()) {

Reads each row of data back in an array form

string[] str = reader.getvalues ();

if (str! = null && str.length > 0) {

if (str[0]! = NULL &&! "". Equals (Str[0].trim ())) {

List.add (str);

}

}

}

} catch (FileNotFoundException e) {

Log.error ("Error reading CSV file.", E);

} catch (IOException e) {

Log.error ("", e);

}

finally{

if (reader! = null)

Close Csvreader

Reader.close ();

}

return list;

}

The above code has several points:

1 Specify delimiter and character encoding when initializing Csvreader, if not specified, the default is comma and iso-8859-1, I use GBK, depending on the character encoding at the time.

2 reads each row of data, returns an array of strings, the order in which the file data columns are ordered

3 finally remember to close Csvreader

is not very simple, the returned array format is exactly what I want, and get the original data, no special character processing.

Some children's shoes questioned the special character unhandled, inserted into the database will be wrong, in fact, we do not need to handle the manual, some of the basic components such as JDBC PreparedStatement has been included in the processing of special characters, we just as a binding parameter in the form of the transfer of the data containing special characters can be. The common persistence Framework also encapsulates JDBC at the bottom, and it naturally handles special characters.

If you do not know the friend can add me Q, or add group number to study together, we learn to share video programming, hope to help like Java friends. If you need any help, you can contact me.

On Java read CSV practice

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.