我想在當(dāng)前不存在的文件夾中寫入一個(gè)新文件。我像這樣使用它:File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");file.getParentFile().mkdirs();FileWriter writer = new FileWriter(file);我filename.txt在一個(gè)名為dir1.dir2.我需要dir1/dir2:我怎樣才能做到這一點(diǎn)?請(qǐng)不要將此問題標(biāo)記為重復(fù),因?yàn)槲以谘芯亢鬀]有得到我需要的東西。更新 1我正在使用帶有 Spring Boot 的 Jasper Report 導(dǎo)出 pdf 文件。我需要在目錄名稱下創(chuàng)建文件current year. 在這個(gè)文件夾下,我需要?jiǎng)?chuàng)建一個(gè)名為 的目錄the current month,在這個(gè)目錄下應(yīng)該導(dǎo)出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, "");這是我得到的:
3 回答

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
嘗試下面的代碼,它將按照您的期望工作
File dir = new File("C:\\Users\\username\\Desktop\\dir1\\dir2");
dir.mkdirs();
File file = new File(dir, "filename.txt");
FileWriter newFile = new FileWriter(file);
您需要先創(chuàng)建文件夾結(jié)構(gòu),然后再創(chuàng)建文件
添加回答
舉報(bào)
0/150
提交
取消