【问题标题】:Create whole path automatically when writing to a new file with sub folders [duplicate]写入带有子文件夹的新文件时自动创建整个路径[重复]
【发布时间】: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 来获取yearmonth

这是我的代码的一部分:

    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 我想我已经解释了一切。
  • 您解释了您的工作。但需要明确你面临的问题
  • 我想你现在可以解决问题了
  • 我给出了答案。只需转到您的项目目录即可找到预期的文件夹。我通过.展示它

标签: java path


【解决方案1】:

试试下面的代码,它会按照你的期望工作

File dir = new File("C:\\Users\\username\\Desktop\\dir1\\dir2");
dir.mkdirs();
File file = new File(dir, "filename.txt");
FileWriter newFile = new FileWriter(file);

你需要先创建文件夹结构,然后文件

【讨论】:

  • 它不起作用。我得到了同样的结果。
  • 但我在本地运行它工作正常。验证目录名称可能有斜线的点瞬间
  • 我正在使用 jasper 报告和 spring boot 并导出为 pdf 文件,可能是因为这个。我更新了问题
  • @AmirChoubani 检查我的答案
  • @AmirChoubani 验证您正在创建文件的路径。上面的代码应该可以工作,它与 boot 或 jasper 您的 using 无关。 Atlast 它将在 JVM 上运行。
【解决方案2】:

你需要先创建目录,然后再创建文件:

像这样:

String dirName = "/" + localDateTime.getYear() + "/" + localDateTime.getMonth().name();
    File file = new File(dirName);
    file.mkdirs();
    file = new File(file.getAbsolutePath()+"/quriReport.pdf");
    file.createNewFile();

如果你去你的项目目录,你会看到2018/August/quriReport.pdf

但如果只有one subfolderIDE 显示带有. 的子文件夹。

【讨论】:

  • 是的,就是这样。这是 IDE 显示子文件夹的方式:)
【解决方案3】:

首先,创建目录,然后创建一个要写入的新文件。

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

class TestDir {
    public static void main(String[] args) {
        String dirPath = "C:\\user\\Desktop\\dir1\\dir2\\";
        String fileName = "filename.txt";

        Path path = Paths.get(dirPath);
        if(!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(dirPath + fileName), "utf-8"))) {
            writer.write("something");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

  • 既然您已经完全改变了已删除答案的答案,您正在使用与Files.createDirectories 给出的完全相同的建议...
  • @Eugene 是的,我的可能更容易出错
  • uuuups,我的坏,没注意
【解决方案4】:
File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
Files.createDirectories(file.getParentFile().toPath());
FileWriter writer = new FileWriter(file);
writer.write("jora");
writer.close();

【讨论】:

  • 我没有得到预期的结果。
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2017-08-06
  • 2019-09-25
相关资源
最近更新 更多