Date dir = new java.util.Date(System.currentTimeMillis());
String baseDir = "/home/gaurav/usr/logs/ESBegin/";
String newDir = createDateBasedDirectory(baseDir, dir);
Logger logger = Logger.getLogger("MyLog1");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(newDir+"/data.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info(stringifiedJson);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这将创建一个今天日期的文件夹,但是我想为每天创建一个新文件夹并将日志文件存储在新文件夹中。...意味着每天的文件夹必须具有当天的日志文件 我有以下创建文件夹的方法
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
File f = new File(baseDirectory);
File files[] = f.listFiles();
String dir = null;
int baseDirLength = baseDirectory.length();
int checkingLength = baseDirLength + format.length() + 3;
int maxSeqNo = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
dir = files[i].toString();
if (dir.length() == checkingLength) {
String existingDirDate = dir.substring(baseDirLength, baseDirLength + 10);
if (date.equalsIgnoreCase(existingDirDate)) {
int dirSeqNo =
Integer.parseInt(dir.substring(baseDirLength + 11, baseDirLength + 10 + 3));
if (dirSeqNo > maxSeqNo) {
maxSeqNo = dirSeqNo;
}
}
}
}
}
String currSeq = "" + (maxSeqNo + 1);
if (currSeq.length() == 1) {
currSeq = "0" + currSeq;
}
newDir = baseDirectory + date;
new File(newDir).mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
如果要每天创建一个新文件夹,我应该怎么做
答案 0 :(得分:1)
您可以使用日志记录框架的功能来执行此操作。例如,使用Log4J's RollingFileAppender。
您可以使用fileName
参数来创建新目录。从documentation:
fileName:要写入的文件的名称。如果该文件或其任何父目录不存在,则将创建它们。
答案 1 :(得分:0)
检查当前目录是否存在,如果不存在则创建该目录,或者如果存在则直接返回。
/*
* to make sure everyone knows what's going on
* name this method like getOrCreateDailyLogDirectory
*/
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
// check if the directory exists:
String todaysLogDir = baseDirectory + "\\" + date; // create the path as String
// then create a Path (java.nio, alternatives possible)
Path todaysDirectoryPath = Paths.get(todaysLogDir);
// and check if this Path exists
if (Files.exists(todaysDirectoryPath) {
// if present, just return it in order to write (into) a log file there
return todaysDirectoryPath.toUri().toString();
} else {
// create it the way you want and return the path as String
...
}
...
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
以这种方式(或类似方式)执行操作始终会返回当天的目录,并在每天第一次尝试登录之前每天创建一次该目录。