java--read CSV file and derivative

Source: Internet
Author: User

 PackageCom.datahander;ImportJava.io.File;ImportJava.nio.charset.Charset;Importjava.util.ArrayList;Importjava.util.List;ImportCom.csvreader.CsvReader;ImportCom.util.Manager; Public classReadcsv {Private Static BooleanFlag =false;; /*** CSV file derivative *@paramfile*/     Public Static voidcvsmanager (file file) {if(File.exists () | | file.length () > 0) {flag=Ispics (File.getname ()); if(!flag) {System.out.println ("File Format error"); }Else{                Try{List<String[]> csvlist =NewArraylist<string[]> ();//used to save dataString Csvfilepath =File.getpath (); Csvreader Reader=NULL; Try{Reader=NewCsvreader (Csvfilepath, ', ', Charset.forname ("UTF-8")); //reader.readheaders (); //Skip the header if you need a table header, don't write this sentence.                         intLinesum = 0; intCount = 1000;  while(Reader.readrecord ()) {//read rows of data in addition to the headerLinesum + +; String s[]=reader.getvalues ();  for(inti=0;i<s.length;i++) {S[i]=s[i].replaceall ("'", "" "); S[i]=s[i].replaceall ("\\\\", "/");                            } System.out.println (s);                              Csvlist.add (s); if(linesum% Count = = 0) {importdatatomysql (csvlist); //method of calling derivativeSystem.out.println ("1000 Inserted");                            Csvlist.clear ();                        }} importdatatomysql (Csvlist);                        Csvlist.clear (); System.err.println ("Insert complete!" "); }finally{reader.close (); }                    }Catch(Exception ex) {System.out.println (ex); }            }        }Else{System.err.println ("The file does not exist or the file is empty!"); }    }    /*** Import the collection into the database *@paramcsvlist*/     Public Static voidImportdatatomysql (list<string[]>csvlist) {String va= ""; String Table= "Cvsdatas"; String Val= "(Company_name,cate_name,province,city,area,address,contact_people,contact_phone,telephone,website,email, LATITUDE,LONGITUDE,PRODUCT,DESCRIPTION,LOGO,COMPANY_ID) ";  for(intRow=0;row<csvlist.size (); row++) {VA+ = "('" + csvlist.get (row) [0]+ "', '" +csvlist.get (row) [1]+ "', '" +csvlist.get (row) [2]+ "', '" +csvlist.get (row) [3]+ "', '") +csvlist.get (Row) [4]+ "', '" +csvlist.get (row) [5]+ "', '" +csvlist.get (row) [6]+ "', '" +csvlist.get (row) [7]+ "', '" + Csvlist.get (Row) [8]+ "', '" +csvlist.get (row) [9]+ "', '" +csvlist.get (row) [10]+ "', '" +csvlist.get (row) [11]+ "', '" + Csvlist.get (Row) [12]+ "', '" +csvlist.get (row) [13]+ "', '" +csvlist.get (row) [14]+ "', '" +csvlist.get (row) [15]+ "', '" + Csvlist.get (Row) [16]+ "'),"; } VA=va.substring (0, Va.length ()-1);      Manager.insertdata (Table,va,val); }     Public Static Booleanispics (String filename) {BooleanFlag =false; if(Filename.endswith (". csv")) Flag=true; returnFlag; }}

java--read CSV file and derivative

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.