java map讀取csv文件
阿新 • • 發佈:2019-02-12
num csv文件 reat ice -s inf sta ora get
public void import5(){ //解析xlsx文件 String path="E:\\bbox_list.xlsx"; Map<String, List<List<String>>> columnMap = null; try { Workbook wb = new XSSFWorkbook(new FileInputStream(new File(path))); Sheet st = wb.getSheetAt(0);if(st != null){ columnMap = new HashMap<String, List<List<String>>>(st.getLastRowNum());//定義columnMap,存放圖及它的分類和框 System.out.println(st.getLastRowNum()); System.out.println("--------------st.getLastRowNum()-------------");for(int i=0;i<=st.getLastRowNum();i++){ Row row = st.getRow(i); List<List<String>> informationLists = new ArrayList<List<String>>();//定義大list List<String> informationList = new ArrayList<String>(); //定義小list if(row != null){ int rows = row.getLastCellNum();//每行有6列 if(rows>0){ for(int j=0;j<rows;j++){ Cell cell = row.getCell(j); informationList.add(cell.toString());//內層小list取到每一列的值 } if(columnMap.containsKey(informationList.get(0))){//存進columnMap中;如果columnMap已經存在該圖,在它的信息上追加新list List<List<String>> value = columnMap.get(informationList.get(0)); value.add(informationList); columnMap.put(informationList.get(0), value); }else{ //存進columnMap中 informationLists.add(informationList); columnMap.put(informationList.get(0), informationLists); } } } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("文件未找到.." + e.getClass().getName() + "\t 信息:" + e.getMessage()); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Io異常:" + e.getClass().getName() + "\t 信息:" + e.getMessage()); e.printStackTrace(); } System.out.println(columnMap); //上傳圖像並分類並帶框 //取到文件夾下分類的所有圖片路徑,並取到圖片的MD5---------------------- FolderUtil fu=new FolderUtil(); List<String> imgList=fu.refreshFileList("D:\\a", "jpg"); int countadd=0; //Map<String,String> jpgnamelist = new HashMap<String,String>();//名字已存在,或者MD5已存在(不明顯),讀取原名字圖片地址,不上傳新的 for(String imgUrl:imgList){ String jpgname = (imgUrl.split("\\\\")[2]);//.split("\\.")[0];//取到圖片名字 System.out.println(jpgname); //打印圖片名字 //取到圖片的MD5 File imgFile = new File(imgUrl); //生成縮略圖 InputStream in = null; try { in = new FileInputStream(imgFile); String fileMd5Name=UploadFile.getFileMD5(in); if(in!=null) in.close(); //上傳原圖 String opath=this.upFile(imgUrl, "jpg"); //縮略圖 File simgFile=new File(imgUrl); Map<String,Object> map=ImageZipUtil.zipImageFileByte(simgFile,310, 0, 1f); byte[] bt = (byte[]) map.get("bt"); ByteArrayInputStream bin = new ByteArrayInputStream(bt); String spath= this.upFile(bin, bt.length, "jpg"); System.out.println(columnMap.get(jpgname)); //打印map裏圖片對應value int listcount=columnMap.get(jpgname).size(); for(int i=0;i<listcount;i++){ List<List<String>> outLists = columnMap.get(jpgname);//定義大list List<String> inList = outLists.get(i); System.out.println(inList); //打印當前小list String TypeId=""; System.out.println(inList.get(1)); //打印分類名 JSONObject obj = new JSONObject(); obj.put("x", Double.valueOf(inList.get(2))); obj.put("y", Double.valueOf(inList.get(3))); obj.put("width", Double.valueOf(inList.get(4))); obj.put("height",Double.valueOf(inList.get(5))); JSONArray arrayNew=new JSONArray(); arrayNew.add(obj); System.out.println(arrayNew); //打印json switch (inList.get(1)) { case "Atelectasis": //積液 TypeId="64c1282028484899a9e6dcff5c0a71df"; System.out.println("Atelectasis-------------------------"); break; case "Cardiomegaly": //皮下積氣 TypeId="254d4860e735438e9525981ccf814277"; break; case "Effusion": //氣胸 TypeId="b11665be08c34045a926671011f1ffd5"; break; case "Infiltrate": //陳舊 TypeId="b25b7ca9aeb5467d94279d49b24bb441"; break; case "Mass": //正常: TypeId="89d1e0832a184c4f92cb233d4cd55a1f"; break; case "Nodule": //滲出 TypeId="9bb7e278fb3a4f75adf03c7f28f23aa4"; break; case "Pneumonia": //滲出 TypeId="d5d96332d1c74dddbc79b1ed9b17d9e2"; break; case "Pneumothorax": //滲出 TypeId="34dd7e4a16de421c8cd1d73f22f19008"; break; default: System.out.println(jpgname + " 異常位置: " + inList.get(1)); //return null; } TagImg newImg=new TagImg(); newImg.setImgMd5(fileMd5Name); newImg.setImgUrl(opath); newImg.setName(jpgname); newImg.setImgUrlThumb(spath); newImg.setCreateUser("yl-921"); newImg.setOrgId("81dae004980145ffbd261717b49f35b6"); newImg.setSetId("e2bf364d87854293a374b49d2609d9c7"); newImg.setTypeId(TypeId); newImg.setTagFlag(1); newImg.setTagJson(arrayNew.toString()); //newImg.setTagJson(tagJson); countadd++; System.out.println(countadd); tagImgService.save(newImg); } bin.close(); bt=null; } catch (Exception e) { // TODO: handle exception } } System.out.println("共: "+imgList.size()+" 本次添加:"+countadd); }
java map讀取csv文件