【java小工具】批量處理txt文件中的數據

   /**
     * 批量更新
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateNumber")
    public void updateNumber() {
        String seqId = UUID.randomUUID().toString();
        //設置所需參數
        //subts,變更時間,ts
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
        String ts=sdf.format(new Date());
        String subts=sdf2.format(new Date());
        XzDataBindVo bindVo=new XzDataBindVo();
        bindVo.setTs(ts);
        bindVo.setSubts(subts);
//        bindVo.setSubId("C1129X028X0278563933-00-0-XZGF-GXI");
        bindVo.setSmsmtchannel("4");
        //讀取數據
        //String filename = "/src/main/resources/data.txt";
		String path =this.getClass().getClassLoader().getResource("./data.txt").getPath();
		Map<Integer, String> fileMaps = readTxtFile(path);
		int num = fileMaps.size();// 行數
		for (int i = 0; i < num; i++) {
			String subId = fileMaps.get(i);
            bindVo.setSubId(subId);
            //調用修改接口
            try {
               xzBindService.updateBind(loggerSingleWork, bindVo, seqId);
            } catch (Exception e) {
				System.out.println("catch a exception");
            }
		}
    }
    public static Map<Integer,String> readTxtFile(String filePath) {

        //存放內容的map對象
        Map<Integer,String> filemaps = new HashMap<Integer,String>();
        try {
            String encoding = "GBK";
            File file = new File(filePath);
            int count = 0;//定義順序變量
            if (file.isFile() && file.exists()) { // 判斷文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考慮到編碼格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {//按行讀取
                    if(!"".equals(lineTxt)){
                        String reds = lineTxt.split("\\+")[0];//對行的內容進行分析處理後再放入map裏。
                        filemaps.put(count, reds);//放入map
                        count ++;
                    }
                }
                read.close();//關閉InputStreamReader
                bufferedReader.close();//關閉BufferedReader
            } else {
                System.out.println("not find");
            }
        } catch (Exception e) {
            System.out.println("wrong");
            e.printStackTrace();
        }
        return filemaps;
    }

}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章