Java javacsv. jar application for csv file operations, csvjavacsv. jar
Csv files are separated files. If you use java I/O streams for writing, it is troublesome. Here we provide you with a jar package of javacsv, which is very convenient for you to operate csv files.
: Https://pan.baidu.com/s/1i46ErFB
So how to use it?
Take a look at the next example and you will understand it.
1 import com.csv reader. csvReader; 2 import com.csv reader. csvWriter; 3 import org. junit. test; 4 5 import java. io. IOException; 6 import java. nio. charset. charset; 7 8/** 9 * Created by javalittleman on 2016/8/18. 10 */11 public class TestCVS {12/** 13 * CSV export 14*15 * @ throws Exception16 */17 @ Test18 public void exportCsv () throws IOException {19 String srcCSV = "F:/cnt_programa.csv"; 20 String targetFi Le = "F:/test.csv"; 21 CsvReader reader = new CsvReader (srcCSV, ',', Charset. forName ("UTF-8"); 22 CsvWriter write = new CsvWriter (targetFile, ',', Charset. forName ("UTF-8"); 23 // each field is marked with 24 write. setForceQualifier (true); 25 // pass through header 26 // r. readHeaders (); 27 // read records one by one until 28 String [] header ={}; 29 while (reader. readRecord () {30 // Save the head 31 if (reader. getCurrentRecord () = 0) {32 header = reader. getValues (); 33} 34 // obtain the current record location 35 System. out. print (reader. getCurrentRecord () + ". "); 36 // read a record 37 System. out. println (reader. getRawRecord (); 38 String [] tmp = {reader. getValues () [0], reader. getValues () [1]}; 39 // modify the record and write only the first field and the Second Field 40 if (! Header [1]. equals (tmp [1]) & ("". equals (tmp [1]) | tmp = null) {41 tmp [1] = "null"; 42 write. writeRecord (tmp); 43} else {44 write. writeRecord (new String [] {reader. getValues () [0], reader. getValues () [1]}); 45} 46} 47 reader. close (); 48 write. close (); 49} 50}
Cnt_programa.csv file:
"Id", "pid", "no", "serial", "name", "createtime" "100000", "", "No100000 ","", "company news", "17:12:09" "100001", "", "No100001", "", "hot news", "17:12:36" "100046", "100001 ", "No100046", "1", "Bank Dynamics", "10:36:31" "100052", "100001", "No100052", "2", "Laws and Regulations ", "20:39:10" "100088", "100001", "No100088", "3", "professional library", "19:05:47"
test.csv
"Id", "pid" "100000", "EMPTY" "100001", "EMPTY" "100046", "100001" 100052 "," 100001 "" 100088 ", "100001"