java streamreader a file

  /***********************************************************************
   * update product category
   **********************************************************************/
  if (overview) {
   try {
    
    String filepath = pathProp.get("xml_path") + "en//category_xml//gps-navigation-products.xml";
    String filepath2 = pathProp.get("xml_path") + "en//category_xml//gps-navigation-products.xml.tmp";
    System.out.println(filepath);

    File inputFile = new File(filepath);
    File outputFile = new File(filepath2);
    if (inputFile.exists()) {
     FileInputStream inputStream = new FileInputStream(inputFile);
     InputStreamReader inputStreamRead = new InputStreamReader(
       inputStream, "utf-8");
     BufferedReader br = new BufferedReader(inputStreamRead);

     if (inputStream.read() != 0x3C) { // (0x3C + <)
      FileOutputStream fs = new FileOutputStream(outputFile);
      OutputStreamWriter osw = new OutputStreamWriter(fs,
        "utf-8");
      BufferedWriter stdout = new BufferedWriter(osw);
      char c[] = new char[20 * 1024];
      int byteread = 0;
      while ((byteread = br.read(c)) > 0) {
       //System.out.println(byteread);
       stdout.write(c, 2, byteread - 2);
       stdout.flush();
      }
      c = null;
      fs.close();
      osw.close();
      stdout.close();
     }
     inputStream.close();
     inputStreamRead.close();
     br.close();

     
     SAXReader reader = new SAXReader();
     FileInputStream fis = null;
     if(outputFile.exists()){
      fis = new FileInputStream(outputFile);
     }else{
      fis = new FileInputStream(inputFile);
     }
     
     //SAXReader reader = new SAXReader();
     Document doc = reader.read(fis);
     Element root = doc.getRootElement();
     Element catEle;
     Element foo;

     String category_name = "";
     int category_id = 0;
     String product_name = "";
     int product_id = 0;

     List categoryList = root.elements();
     for (int j = 0; j < categoryList.size(); j++) {
      catEle = (Element) categoryList.get(j);
      category_name = catEle.attributeValue("name");
      category_id = MultiDesign
        .getCategoryIdByName(category_name);
      System.out.println(category_name + ": " + category_id);
      for (Iterator i = catEle.elementIterator("Item"); i
        .hasNext();) {
       // System.out.println("RUN ELEMENT!!!");
       foo = (Element) i.next();
       product_name = foo.elementText("name");
       product_id = Product
         .getProductIdByName(product_name);
       System.out
         .println(product_name + ": " + product_id);
       Product.addProdcutCategory(category_id, product_id);
      }
     }
     //inputFile.delete();
     outputFile.delete();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值