⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.java

📁 java支持的短信平台
💻 JAVA
字号:
package com.khan.file;


import java.io.*;
import java.util.*;
import com.khan.datetime.*;

public class Log {
  protected String sFileName; //文件名
  protected PrintWriter pwLog = null;


  /**
   * 访问日志记录
   * @param fileName String 文件名
   */
  public Log(String fileName) {
    sFileName = fileName;
  //  bakLog();
  }

  /**
   * 取得文件最后修改时间
   * @return long
   */
  public long fileIsNow() {
    Calendar now = Calendar.getInstance();
    now.setTimeInMillis(new File(sFileName).lastModified());
  //  now.setTimeZone(TimeZone.getDefault());
  //  now.set(2004,8-1,30,23,59,59);
  //  new File(sFileName).setLastModified(now.getTimeInMillis());
    long i= now.get(Calendar.DATE) + 100L * (now.get(Calendar.MONTH)+1) + 10000L * now.get(Calendar.YEAR);
    return i;
  }

  public static long fileSize(String fileName) {
    return new File(fileName).length();
  }


  /**
   * 文件是否存在
   * @param fileName String
   * @return boolean
   */
  synchronized public static boolean isFileExist(String fileName) {
      File file = new File(fileName);
      boolean  f = file.exists();
      //file.clo
      return f;
  }

  /**
   * 输出日志
   * @param out String
   */
  synchronized public void logOut(String out) {
    if (fileIsNow() != SMPTime.getDay(false)) {
      close();
      bakLog();
    }

    if (pwLog == null)   openWriter(true);

    String msg = SMPTime.getDateTime(true) + ":" + Thread.currentThread().getName() +":" + out;
    pwLog.println(msg);
  }

  /**
   * 输出错误信息
   * @param out String
   * @param e Exception
   */
  synchronized public void logOut(String out, Exception e) {
    if (fileIsNow() != SMPTime.getDay(false)) {
      close();
      bakLog();
    }

    if (pwLog == null)  openWriter(true);

    String msg = SMPTime.getDateTime(true) + ":" + Thread.currentThread().getName() +":" + out;
    pwLog.println(msg);

    pwLog.println(e.getMessage());
    e.printStackTrace(pwLog);
  }

  protected void openWriter(boolean append) {
    try {
      pwLog = new PrintWriter(new FileWriter(sFileName, append), true);
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
    }
  }


  /**
   * 关闭文件
   */
  public void close() {
    if (pwLog != null) {
      pwLog.close();
      pwLog = null;
    }
  }

  /**
   * 备份日志文件
   */
  protected void bakLog() {
    try {
      String toFile = "log" + File.separator + sFileName + fileIsNow();
      (new File(sFileName)).renameTo(new File(toFile));
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
    }
  }

  protected void finalize(){
    close();
  }

  public static void main(String[] args) {
    //Log logout = new Log("out");
    //Log logerr = new Log("err");
   //  logout.fileIsNow();
   //  logerr.fileIsNow();
    //System.out.println( logerr.fileSize("http://www.sina.com.cn/index.html"));
    //System.out.println("out:"+logout.fileIsNow()+" now:"+SMPTime.getDay(false));
    //while (true) {
     //  logout.logOut("test");
    //  logerr.logOut("err");
    //  com.khan.util.common.sleep(1000);
    //}






   //System.out.println( System.);
    Runtime rt = java.lang.Runtime.getRuntime();
    Process pr = null;
    String strs[] = new String[1];
    strs[0] = "help";
    File f = new File("D:\\Program Files\\gawk\bin");

    try {
        pr=rt.exec("gawk", strs , f);
    } catch (IOException e) {
        e.printStackTrace();
    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -