public class Goods { private String name; private int type; private Double price; public Goods(){ } public Goods(String name, int type, Double price) { super(); this.name = name; this.type = type; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } }
public class Demo { public static void fun() { String path = "csv/4399.csv"; List<Goods>list = new ArrayList<Goods>(); try { CsvReader reader = new CsvReader(path, ‘\t‘, Charset.forName("UTF-16LE")); reader.readHeaders();// 跳过表头 如果需要表头的话,不要写这句。 reader.readHeaders(); reader.readHeaders(); int name_pos = 0; //商品名称位置 int type_pos = 1; //商品类型位置 int price_pos = 2; //商品价格位置 while (reader.readRecord()) { // 逐行读入除表头的数据 Goods goods = new Goods(); 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 void main(String[] args) { fun(); } }
时间: 2024-10-08 20:38:24