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(); } 這將創(chuàng)建一個今天的文件夾,但是我想為每天創(chuàng)建一個新文件夾并將日志文件存儲在新文件夾中....意味著每天的文件夾必須具有當天的日志文件,我具有以下創(chuàng)建文件夾的方法 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);如果要每天創(chuàng)建一個新文件夾,我應該更改什么?
如何每天以其名稱作為當前日期創(chuàng)建新目錄
慕田峪9158850
2021-04-07 11:15:55