夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          package com.roadway.edmail.fboperate;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.util.ArrayList;
          import java.util.List;

          import org.apache.log4j.Logger;

          import com.roadway.edmail.fboperate.model.OperateModel;
          import com.roadway.edmail.util.StrUtil;
          import com.roadway.edmail.util.SysParameters;

          public class OperateModelCacheHelper
          {
              
          /** 日志記錄 */
              
          private static Logger logger = Logger
                      .getLogger(OperateModelCacheHelper.
          class.getName());
              
              
          /** 未處理的操作模型的文件的路徑 */
              
          private static String operatedModelPath = SysParameters.basicPath
                      
          + "operatingModel.txt";;
              
              
          /** 已經(jīng)處理的操作模型的文件的路徑 */
              
          private static String operatingModelPath = SysParameters.basicPath
                      
          + "operatedModel.txt";
              
              
          /**
               * 本方法用于把一個操作模型存入到臨時文件
               * 
               * 
          @param filePath
               *            文件的路徑
               * 
          @param model
               *            對像模型
               
          */
              
          public static synchronized void model2File(String filePath,
                      OperateModel model, 
          boolean isAppend)
              {
                  
          /** 如果對象為NULL */
                  
          if (model == null)
                  {
                      
          return;
                  }
                  File operatingFile 
          = new File(filePath);
                  
          /** 如果文件不存在,退出程序 */
                  
          if (!operatingFile.exists())
                  {
                      
          return;
                  }
                  FileOutputStream fos 
          = null;
                  
          try
                  {
                      fos 
          = new FileOutputStream(operatingFile, isAppend);
                      fos.write(model.toString().getBytes());
                  }
                  
          catch (Exception ex)
                  {
                      
          /** 把異常信息加入到日志 */
                      OperateModelCacheHelper.logger.error(StrUtil.getExceptionInfo(ex,
                              
          "edm_h"));
                  }
                  
          finally
                  {
                      
          try
                      {
                          
          /** 關(guān)閉輸入輸出流 */
                          fos.close();
                      }
                      
          catch (Exception e)
                      {   

                      }
                  }
              }
              
              
          /**
               * 
          @param filePath
               * 
          @return
               
          */
              
          public static synchronized List<OperateModel> file2Models(String filePath)
              {
                  
          /** 裝models的集合 */
                  List
          <OperateModel> models = new ArrayList<OperateModel>();
                  File operatingFile 
          = new File(filePath);
                  
          /** 如果文件不存在,退出程序 */
                  
          if (!operatingFile.exists())
                  {
                      
          return models;
                  }
                  StringBuffer content 
          = new StringBuffer();
                  FileInputStream fis 
          = null;
                  
          try
                  {
                      fis 
          = new FileInputStream(operatingFile);
                      
          byte[] buffer = new byte[1024];
                      
          int byteRead = 0;
                      
          while ((byteRead = fis.read(buffer, 0, buffer.length)) != -1)
                      {
                          content.append(
          new String(buffer, 0, byteRead));
                      }
                  }
                  
          catch (Exception ex)
                  {
                      
          /** 把異常信息加入到日志 */
                      OperateModelCacheHelper.logger.error(StrUtil.getExceptionInfo(ex,
                              
          "edm_h"));
                  }
                  
          finally
                  {
                      
          try
                      {
                          fis.close();
                      }
                      
          catch (Exception ex)
                      {}
                  }
                  String contentStr 
          = content.toString();
                  
          /** 記錄日志 */
                  OperateModelCacheHelper.logger.debug(
          "file2Model的內(nèi)容為:" + contentStr);
                  String[] strModels 
          = contentStr.split("\n\r");
                  
                  OperateModel model 
          = null;
                  
          for (int len = 0; len < strModels.length; len++)
                  {
                      String[] infos 
          = strModels[len].split("[,]");
                      
          if (infos.length == 3)
                      {
                          model 
          = new OperateModel(infos[0], infos[1], infos[3]);
                          models.add(model);
                      }
                  }
                  
          return models;
              }
          }
          posted on 2008-11-04 18:08 HUIKK 閱讀(169) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 平武县| 海阳市| 大姚县| 新平| 八宿县| 焉耆| 炉霍县| 临安市| 原平市| 平舆县| 兴业县| 娱乐| 阜康市| 杭锦旗| 桃园市| 彝良县| 西宁市| 宜宾市| 崇义县| 临安市| 皋兰县| 文山县| 濮阳市| 吉林省| 青神县| 海门市| 视频| 信阳市| 宝清县| 海南省| 平山县| 通山县| 昭苏县| 武川县| 娄底市| 鄂温| 娱乐| 东光县| 无极县| 临城县| 定州市|