Public classGoods {PrivateString name; Private inttype; PrivateDouble Price; PublicGoods () {} PublicGoods (String name,inttype, Double price) { Super(); This. Name =name; This. Type =type; This. Price =Price ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetType () {returntype; } Public voidSetType (inttype) { This. Type =type; } PublicDouble GetPrice () {returnPrice ; } Public voidSetprice (Double price) { This. Price =Price ; } }
Public classDemo { Public Static voidFun () {String path= "Csv/4399.csv"; List<goods>list =NewArraylist<goods>(); Try{Csvreader Reader=NewCsvreader (path, ' \ t '), Charset.forname ("Utf-16le")); Reader.readheaders ();//Skip the header if you need a table header, don't write this sentence. reader.readheaders (); Reader.readheaders (); intName_pos = 0; //Product name Location intType_pos = 1; //Product type location intPrice_pos = 2; //Commodity price location while(Reader.readrecord ()) { //row-by-line reading of data except header Goods Goods=NewGoods (); Goods.setname (Reader.get (Name_pos)); Goods.settype (Integer.parseint (Reader.get (Type_pos))); Goods.setprice (Double.parsedouble (Reader.get (Price_pos))); List.add (goods); } reader.close (); } Catch(IOException e) {e.printstacktrace (); } for(Goods goods:list) {System.out.println (Goods.getname ()); System.out.println (Goods.gettype ()); System.out.println (Goods.getprice ()); } } Public Static voidMain (string[] args) {fun (); }}
CSV file read