日志记录类(将日志保存在一个文件中)

package com.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class Log {

 private String filepath = "C:\\Documents and Settings\\Administrator\\桌面\\tmp";
 
 public Log() {
 }

 // public static void log(int i, int level) {
 // log("" + i, level);
 // }

 // public static void log(String info, int level) {
 // // if (level >= Config.getInstance().getLogLevel())
 // logf(info, level);
 // }

 // public static void log(String info) {
 // log(info, 0);
 // }

 // public static void log(int info) {
 // log(info, 0);
 // }

 // private static synchronized void logf(String info, int level) {
 // infoBuffer.delete(0, infoBuffer.length());
 // time.setTime(System.currentTimeMillis());
 // infoBuffer.append("[").append(level).append("]")
 // .append(time.toString()).append("\t").append(info);
 // Config.getInstance().getLogArea().append("\r\n");
 // Config.getInstance().getLogArea().append(infoBuffer.toString());
 // writeFile(infoBuffer.toString());
 // }

 // public static void log(Exception e) {
 // log(e.getMessage(), 2);
 // if (Config.getInstance().getLogLevel() <= 2) {
 // FileOutputStream fos = null;
 // try {
 // fos = new FileOutputStream(Config.getInstance().getPath()
 // + "log" + File.separator + "log.txt", true);
 // e.printStackTrace(new PrintStream(fos));
 // } catch (Exception ex) {
 // ex.printStackTrace();
 // } finally {
 // if (fos != null)
 // try {
 // fos.close();
 // } catch (Exception es) {
 // es.printStackTrace();
 // }
 // }
 // }
 // }

 // private static void writeFile(String message) {
 // writeFile(message, Config.getInstance().getPath() + "log"
 // + File.separator + "log.txt");
 // }

 public static void writeFile(String message, String path) {
  writeFile(message, path, true);
 }

 public static void writeFile(String message, String path, boolean append) {
  FileWriter os = null;
  try {
   //File file = new File(path.substring(0, path.lastIndexOf("\\")));
   //java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   //java.util.Date utilDate = new java.util.Date();
   String separator= System.getProperty("file.separator");
   File file = new File(path.substring(0, path.lastIndexOf(separator)));
   if (!file.exists())// 如果不存在就创建
    file.mkdirs();

   os = new FileWriter(path, append);
   //os.write(f.format(utilDate)+"\r\n"+message + System.getProperty("line.separator"));
   os.write(message + System.getProperty("line.separator"));
   Thread.sleep(500);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    os.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }

 public static List readFile(String filename) throws IOException {
  return readFile(filename, 0, 0x7fffffff);
 }

 public static List readFile(String filename, int beginPos, int endPos)
   throws IOException {
  List list = new ArrayList(100);
  BufferedReader reader = null;
  String s = null;
  try {
   reader = new BufferedReader(new InputStreamReader(
     new BufferedInputStream(new FileInputStream(filename))));
   for (int pos = 0; (s = reader.readLine()) != null
     && pos >= beginPos && pos++ < endPos;)
    list.add(s);

  } catch (IOException e) {
   // log(e);
   throw e;
  }
  return list;
 }

 public static void createDictory(String path) {
  File f = new File(path);
  File ft = f;
  for (; !f.exists(); f = f.getParentFile())
   ft = f;

  if (f != ft) {
   ft.mkdir();
   createDictory(path);
  }
 }

 public static void copyFile(String oldPathFile, String newPathFile) {
  InputStream inStream = null;
  FileOutputStream fs = null;
  try {
   int byteread = 0;
   File oldfile = new File(oldPathFile);
   if (oldfile.exists()) {
    inStream = new FileInputStream(oldPathFile);
    fs = new FileOutputStream(newPathFile);
    byte buffer[] = new byte[1444];
    while ((byteread = inStream.read(buffer)) != -1)
     fs.write(buffer, 0, byteread);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    inStream.close();
   } catch (Exception ex) {
   }
   try {
    fs.close();
   } catch (Exception ex) {
   }
  }
 }

 public static final int LEVEL_DEBUG = 0;

 public static final int LEVEL_INFO = 1;

 public static final int LEVEL_ERROR = 2;

 private static Timestamp time = new Timestamp(System.currentTimeMillis());

 private static StringBuffer infoBuffer = new StringBuffer(200);
 
 public static String getRealPath() {
   //String realPath = Log.class.getClassLoader().getResource("").getFile();
   String realPath =new File("").getAbsolutePath();
   java.io.File file = new java.io.File(realPath);
   realPath = file.getAbsolutePath();
   try {
   realPath = java.net.URLDecoder.decode(realPath, "utf-8");
   } catch (Exception e) {
   e.printStackTrace();
   }
   return realPath;
 }
 public static void main(String[] args)
 {
  //String path = "E:\\Tomcat 6.0\\webapps\\Kindergarden\\Upload\\picmanagelog\\bugSql.log";
  //String separator= System.getProperty("file.separator");
  //System.out.println(System.getProperty("line.separator"));
  System.out.println(getRealPath());
  System.out.println("7"+new File("").getAbsolutePath());

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值