diff --git a/src/main/java/com/taobao/profile/utils/DailyRollingFileWriter.java b/src/main/java/com/taobao/profile/utils/DailyRollingFileWriter.java index f0364b2..97b61c2 100644 --- a/src/main/java/com/taobao/profile/utils/DailyRollingFileWriter.java +++ b/src/main/java/com/taobao/profile/utils/DailyRollingFileWriter.java @@ -79,7 +79,7 @@ public DailyRollingFileWriter(String filePath) { rolling(now); } } else { - createWriter(filePath); + createWriter(file); } } @@ -164,7 +164,7 @@ private void rolling(Date now) { File file = new File(fileName); file.renameTo(target); - createWriter(fileName); + createWriter(new File(fileName)); rollingFileName = datedFilename; } @@ -183,11 +183,16 @@ private void createWriter(String filename, boolean append) { /** * 直接覆盖旧文件 - * @param filename + * @param file */ - private void createWriter(String filename) { + private void createWriter(File file) { try { - bufferedWriter = new BufferedWriter(new FileWriter(filename), 8 * 1024); + file = file.getCanonicalFile(); + File parent = file.getParentFile(); + if (parent != null && !parent.exists()) { + parent.mkdirs(); + } + bufferedWriter = new BufferedWriter(new FileWriter(file), 8 * 1024); } catch (IOException e) { e.printStackTrace(); }