写:
package cn.com.crystalnet.common;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
/************************
*
* 记录日志类
* @author 王立壮
*
*/
public class RiZhiDB {
private static PrintWriter pw = null;
public static synchronized void init() {
if (pw == null) {
Calendar CD = Calendar.getInstance();
int YY = CD.get(Calendar.YEAR);
int MM = CD.get(Calendar.MONTH) + 1;
int DD = CD.get(Calendar.DATE);
int HH = CD.get(Calendar.HOUR);
int NN = CD.get(Calendar.MINUTE);
int SS = CD.get(Calendar.SECOND);
String month = String.valueOf(MM);
String date = String.valueOf(DD);
String s =null;
if(MM<10){
month="0"+Integer.valueOf(MM).toString();
}
if(DD<10){
date="0"+Integer.valueOf(DD).toString();
}
s = "中信水晶网" + YY + month + date + ".txt";
try {
String filename= s;
File file = new File("../webapps/CrystalNet/log/"+filename);
FileWriter writer = null;
if(!file.exists()){
System.out.println("++++file++++++"+file.getPath());
System.out.println("++++file++++++"+file.getParent());
System.out.println("++++file++++++"+file.getParentFile());
System.out.println("++++file++++++"+file.getCanonicalPath());
System.out.println("++++file++++++"+file.getAbsolutePath());
if(file.createNewFile()){
writer =new FileWriter(file,true);
}
}else{
writer = new FileWriter(file,true);
}
pw = new PrintWriter(writer,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void debug_log(String s) {
init();
pw.println(s);
pw.flush();
}
public static PrintWriter getPW() {
return pw;
}
public static void close() {
// test();
getPW().close();
}
}
读:
package cn.com.crystalnet.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import cn.com.crystalnet.vo.LogVO;
/**
*
* title:日志查找
* @author Administrator
*
*/
public class Find {
private String[][] str;
private static ArrayList<LogVO> array;
private static ArrayList<File> list;
public static void findfile(File st) {
File[] str = st.listFiles();
int length = str.length;
for (int i = 0; i < length; i++) {
if (str[i].isDirectory()) {
findfile(str[i]);
}
if (str[i].isFile()) {
list.add(str[i]);
}
}
}
public static ArrayList find(String timeFrom, String timeTo) {
array = new ArrayList<LogVO>();
list = new ArrayList<File>();
String time1 = timeFrom.toString().replaceAll("-", "");
String time2 = timeTo.toString().replaceAll("-", "");
Integer i1 = Integer.valueOf(time1);
Integer i2 = Integer.valueOf(time2);
System.out.println(time1 + " " + time2);
BufferedReader br = null;
String s = null;
File file = new File("../webapps/CrystalNet/log/");
System.out.println("++++file++++++"+file.getAbsolutePath());
File[] strfile = file.listFiles();
Find.findfile(file);
// System.out.println(file.getName() + "文件夹下面的所有txt文件为:");
for (Object tem : list) {
File list = (File) tem;
String liststr = list.getName();
System.out.println(list.length());
System.out.println(liststr);
if(!liststr.equals("logging.log")){
String time3 = liststr.substring(5, 13);
Integer i3 = Integer.valueOf(time3);
if ((liststr.matches("中信水晶网.*.txt") && (i3 >= i1 && i3 <= i2))) {
try {
br = new BufferedReader(new FileReader("../webapps/CrystalNet/log/" + liststr));
while ((s = br.readLine()) != null) {
// System.out.println(s);
String[] st = s.split("#");
LogVO vo = new LogVO();
int len = st.length;
for(int i=0 ;i<len;i++){
vo.setDate(st[0]);
vo.setType(st[1]);
vo.setContent(st[2]);
}
array.add(vo);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//System.out.println("时间:" + time3);
}
System.out.println(array.size());
return array;
}
}