1

Here is My WriteFile

WriteFile.writeFile(str, "./test/my.html");

And writeFile() method code

public static void writeFile(String content, String fileName)
  {
    try
    {
      File file = new File(fileName);
      if (!file.exists()) {
        file.createNewFile();
      }
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(content);
      bw.close();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

This Code Working fine With Windows but in Linux I am getting below exception

java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1006)
    at org.sewa.util.WriteFile.writeFile(WriteFile.java:25)
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202

1 Answers1

2

Behavior of createNewFile is the same in Windows and Linux, so most likely the path of the file you're specifing exists in Windows while it does not in Linux. In your example, test/ directory does not exist in Linux in the directory where you're executing the program. If you want to create the whole path, see File#mkdirs.

m0skit0
  • 25,268
  • 11
  • 79
  • 127