您的位置:首页 > 编程语言 > Java开发

java streamreader a file

2007-08-06 13:41 344 查看
  /***********************************************************************

   * 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();

   }

  }

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐