【发布时间】:2018-08-02 09:32:13
【问题描述】:
我想在当前不存在的文件夹中写入一个新文件。
我是这样使用的:
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 文件。
我需要在一个名为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, "");
这是我得到的:
【问题讨论】:
-
你遇到什么错误?
-
@sajib 我想我已经解释了一切。
-
您解释了您的工作。但需要明确你面临的问题
-
我想你现在可以解决问题了
-
我给出了答案。只需转到您的项目目录即可找到预期的文件夹。我通过
.展示它