myXPlanner.java

 package com.hitachi.hscms.util.tools;
/**
 * This file is part of HSCMS project which started by ossl lab of Hitachi(China) Research and Development Corp.
 * HSCMS dedicate to give a whole configuration environment for xp developing process,including auto integration etc. functions.
 * This class is a tool to read and write log file
 * @author Li linlin
 * @date 2006-08-10
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.hitachi.hscms.Messages;

public class LogFileOperator {
 private static Logger logger = Logger.getLogger(LogFileOperator.class);
 public static final String logFilePath = Messages.getInstance().getString("LogFileRoot");
 /**
  * Fuction: To read log file of a pointed task
  * @Param: String , task name
  * @return: String, file content
  * @throws Exception: has catched IOException and FileNotFoundException
  * @throws SQLException: null
  */
 public static String readTaskLog(String taskName)
 {
  String result="";
  logger.info("start read");
  File file = new File(logFilePath+File.separator+taskName+".log");
  logger.info(file.getAbsolutePath());
  if(file.exists())
  { 
   logger.info("file exists");
   try {
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String tmp;
    try {
     while((tmp =br.readLine())!=null)
     {
      logger.info("has read ");
      result += (tmp+System.getProperty("line.separator"));
     }
     br.close();
     fr.close();
     
    } catch (IOException e) {
     logger.error("read file "+file.getAbsolutePath()+" error");
     e.printStackTrace();
    }
   } catch (FileNotFoundException e) {
    logger.error("File " + file.getAbsolutePath()+" not found");
    e.printStackTrace();
   }
   
  }
  return result;
 }
 /**
  * Fuction: To write infomation of a pointed task  into  log file ,each task has a log file name named by taskname
  * and each infomation will be added as a line
  * @Param: String , task name ;String, the information to write to the log file
  * @return: boolean, if success,return true
  * @throws Exception: has catched IOException and FileNotFoundException
  * @throws SQLException: null
  */
 public static boolean writeTaskLog(String info,String taskName)
 {
  
  File file = new File(logFilePath);
  if(!file.exists())
   file.mkdir();
  logger.info("Start write");
  file = new File(logFilePath+File.separator+taskName+".log");
   try {
    FileWriter fw= new FileWriter(file,true);
    BufferedWriter br = new BufferedWriter(fw);
    br.write(info);
    logger.info("Write string "+info + "into file" +file.getAbsolutePath());
    br.newLine();
    br.close();
    fw.close();   
   } catch (IOException e) {  
    e.printStackTrace();
    return false;
   } 
   logger.info("End write");
  return true;
 }
 /**
  * Fuction: Delete all the logs when the system restart
  * @Param: null
  * @return: null
  * @throws Exception: null
  * @throws SQLException: null
  */
 public static void clearLogs()
 {
  File file = new File(logFilePath);
  File[] logs = file.listFiles();
  if(logs!=null && logs.length>0)
  {
   for(int i=0;i<logs.length;i++)
   {
    logs[i].delete();
   }
  }
 }
 /**
  * Fuction: Delete the log of the task when the system restart
  * @Param: null
  * @return: null
  * @throws Exception: null
  * @throws SQLException: null
  */
 public static void clearLogs(String taskName)
 {
  File file = new File(logFilePath+File.separator+taskName+".log");
  if(file.exists())
   file.delete();
 }
 public static boolean editTaskLog(String oldName,String newName)
 {
  File file = new File(logFilePath+File.separator+oldName+".log");
  if(file.exists()){
   return file.renameTo(new File((logFilePath+File.separator+newName+".log")));
  }
   return false;
 }
 public static boolean deleteTaskLog(String name)
 {
  File file = new File(logFilePath+File.separator+name+".log");
  if(file.exists()){
   return file.delete();
  }
   return false;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值