title: Create whole path automatically when writing to a new file with sub folders
body: <p>I want to write a new file inside a folder that currently does not exist.</p>
  
  <p>I use it like this:</p>
  
  <pre><code>File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt");
  file.getParentFile().mkdirs();
  FileWriter writer = new FileWriter(file);
  </code></pre>
  
  <p>I get the file <code>filename.txt</code> under a folder called <code>dir1.dir2</code>.</p>
  
  <p>I need <code>dir1/dir2</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/9HHZa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9HHZa.png" alt="enter image description here"></a></p>
  
  <p>How can I achieve this?</p>
  
  <p>Please do not mark this question as duplicate because I didn't get what I need after a reseach.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I am using Jasper Report with Spring Boot to export a pdf file.</p>
  
  <p>I need to create the file under a directory name the <code>current year</code>. Under this folder, I need to create a directory called <code>the current month</code>, and under this directory the pdf file should be exported. Example :</p>
  
  <blockquote>
    <p>(2018/auguest/report.pdf )</p>
  </blockquote>
  
  <p>I am using <code>LocalDateTime</code> to get  <code>year</code> and <code>month</code></p>
  
  <p>Here is a portion of my code :</p>
  
  <pre><code>    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, "");
  </code></pre>
  
  <p>Here is what I get : </p>
  
  <p><a href="https://i.stack.imgur.com/E9Axr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E9Axr.png" alt="enter image description here"></a></p>