Reading and writing CSV file

 

turn:

 

 

Reading and writing CSV file

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/beyond667/article/details/8991525

   CSV what is and what is the use?

   (Baidu, you will know more) My conclusion is simple CSV file (comma seperated value) that is comma delimited, use Notepad to open the case would be "a", "b", "c" format, of course, It can also be changed.

  Useful is that you can easily import spreadsheets and databases, a row represents one data, it is very attractive features, so that you can bulk import values derived database, although you can also use the default database import and export table, but by csv file you can easily choose to filter export. For example, you write a filter condition after the value of screening into csv file one by one, or the value of a csv file batch import a table, is not very easy to do?

  To read and write CSV files need CSVWriter and CSVReader, of course, you want to import the appropriate package for the job.

Then just add in your maven pom file with project management:

  1. <dependency>
  2. <groupId>net.sf.opencsv</groupId>
  3. <artifactId>opencsv</artifactId>
  4. <version>2.3</version>
  5. </dependency>

The need opencsv package, if useless maven, then you can manually download and then import the project, there was still maven convenient, what needs to pack, direct-Period configuration, maven automatically help you download packages and source code, so you can view the source code learning .

Here it is to generate a file with csvWriter

  1. package com.test;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import au.com.bytecode.opencsv.CSVWriter;
  10.  
  11. public class Test {
  12.  
  13. /**
  14. * @param args
  15. */
  16.  
  17. public static void main(String[] args) throws IOException {
  18. CSVWriter writer = new CSVWriter(new FileWriter(new File("./1.csv")),',');
  19. List<String[]> alList= new ArrayList<String[]>();
  20. List<String> list=new ArrayList<String>();
  21. list.add("aa");
  22. list.add("bb");
  23. list.add("cc");
  24. alList.add( list.toArray(new String[list.size()]));
  25.  
  26. list=new ArrayList<String>();
  27. list.add("dd");
  28. list.add("ee");
  29. list.add("ff");
  30. alList.add( list.toArray(new String[list.size()]));
  31.  
  32. writer.writeAll(alList);
  33. writer.close();
  34. System.out.println(writer.toString());
  35.  
  36. }
  37. }

Open the resulting file found 1.csv

  1. "aa","bb","cc"
  2.  
  3. "dd","ee","ff"
  4.  

The above described code is a little lower, CSVWriter Writer = new new CSVWriter (new new FileWriter (new new File ( "./ 1.csv")), ''); declare a csv file 1.csv, and the separator is ',' of course, you can replace other symbols, the default is a comma. And general flow as written, after writeAll need to flush () or close (), by default it is time to close the first flush, flush or close if the call does not show that these values are not always existed in the cache file writes of.

There are two methods for CSVWriter writeAll write the file (stream)

public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames)
public void writeAll(List<String[]> allLines)

The first look, even resultSet can put into them, the other is written in the above example List <String>, then you need to query the list of values ​​into an array.

The other is CSVReader, and the only example of

  1. public static void main(String[] args) throws IOException {
  2.  
  3. CsvReader reader = new CsvReader("./1.csv ");
  4.  
  5. while (reader.readRecord()) {
  6.  
  7. String str = reader.getRawRecord();
  8. System.out.println(str);
  9.  
  10. }
  11. reader.close();
  12. }
  13. }


And general reading files are not very different, I believe we can understand to see if there is deep understanding of CSVWriter back, I will update this article, we hope everyone will have a little bit of use.

 

Guess you like

Origin www.cnblogs.com/libin6505/p/11654206.html