In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Java reads CSV. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Look at the previous processing method in the project is to directly use 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 ones are:
1. Enclose double quotation marks on string data containing special characters
two。 Precede a single double quotation mark in data with a single double quote
Others.
So the strings read with Java IO are all processed strings, which do not meet the expected requirements in some scenarios. For example, what I need is the original content that doesn't do anything.
Another common file format in the project, Excel, is handled by POI, but POI does not support the Csv format, so javacsv is found.
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 and returns String [] str = reader.getValues () as an array 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 main points:
1 specify the delimiter and character encoding when initializing CsvReader. If not, the default is comma and ISO-8859-1, respectively. I used GBK, depending on the character encoding at that time.
2 read each row of data and return an array of strings. The order within the array is the order of the file data columns.
3 * remember to turn off CsvReader
Is it very simple, the returned array format is exactly what I want, and get the original data, without special character processing.
Some students question that there will be errors when inserting special characters into the database without processing. In fact, it is not necessary for us to handle them manually. Some basic components, such as JDBC's preparedstatement, already contain the processing of special characters. All we have to do is to transfer the data containing special characters in the form of binding parameters. The commonly used persistence framework also encapsulates JDBC at the bottom, and naturally handles special characters.
Thank you for reading! This is the end of this article on "how Java reads CSV". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.