<分区>
我想在当前不存在的文件夹中写入一个新文件。
我是这样使用的:
File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);
我在名为 dir1.dir2
的文件夹下获取文件 filename.txt
。
我需要dir1/dir2
:
我怎样才能做到这一点?
请不要将此问题标记为重复,因为我在研究后没有得到我需要的东西。
更新 1
我正在使用 Jasper Report 和 Spring Boot 来导出 pdf 文件。
我需要在 当年
的目录下创建文件。在此文件夹下,我需要创建一个名为 当前月份
的目录,并在此目录下导出 pdf 文件。示例:
(2018/auguest/report.pdf )
我正在使用 LocalDateTime
获取 year
和 month
这是我的部分代码:
ReportFiller reportFiller = context.getBean(ReportFiller.class);
reportFiller.setReportFileName("quai.jrxml");
reportFiller.compileReport();
reportFiller = context.getBean(ReportFiller.class);
reportFiller.fillReport();
ReportExporter simpleExporter = context.getBean(ReportExporter.class);
simpleExporter.setJasperPrint(reportFiller.getJasperPrint());
LocalDateTime localDateTime = LocalDateTime.now();
String dirName = simpleExporter.getPathToSaveFile() + "/"+
localDateTime.getYear() + "/" + localDateTime.getMonth().name();
File dir = new File(dirName);
dir.mkdirs();
String fileName = dirName + "/quaiReport.pdf";
simpleExporter.exportToPdf(fileName, "");
这是我得到的: