我想在当前不存在的文件夹中写入一个新文件。
我像这样使用它:
File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
file.getParentFile().mkdirs();
FileWriter writer = new FileWriter(file);
我filename.txt在一个名为dir1.dir2.
我需要dir1/dir2:
我怎样才能做到这一点?
请不要将此问题标记为重复,因为我在研究后没有得到我需要的东西。
更新 1
我正在使用带有 Spring Boot 的 Jasper Report 导出 pdf 文件。
我需要在目录名称下创建文件current year. 在这个文件夹下,我需要创建一个名为 的目录the current month,在这个目录下应该导出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, "");
这是我得到的:
拉丁的传说
相关分类