title: Java create file if does not exist
body: <p>In my function I want to read a text file. If file does not exists it will be created. I want to use relative path so if i have  .jar, file will be created in the exact same dir. I have tried <a href="https://stackoverflow.com/questions/9620683/java-fileoutputstream-create-file-if-not-exists">this</a>. This is my function and variable <code>fName</code> is set to <code>test.txt</code></p>
  
  <pre><code>    private static String readFile(String fName) {
      String noDiacText;
      StringBuilder sb = new StringBuilder();
      try {
          File f = new File(fName, "UTF8");
          if(!f.exists()){
              f.getParentFile().mkdirs();
              f.createNewFile();
          }
  
          FileReader reader = new FileReader(fName);
          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fName), "UTF8"));
  
          String line;
  
          while ((line = bufferedReader.readLine()) != null) {
              sb.append(line);
  
          }
          reader.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  
      return sb.toString();
  }
  </code></pre>
  
  <p>I am getting an error at <code>f.createNewFile();</code> it says</p>
  
  <pre><code>java.io.IOException:  System cannot find the path specified
  at java.io.WinNTFileSystem.createFileExclusively(Native Method)
  at java.io.File.createNewFile(File.java:1012)
  at main.zadanie3.readFile(zadanie3.java:92)
  </code></pre>