0
  //String s = nameOfFile
  String filePath = System.getProperty("user.dir") + File.separator + ".." + File.separator + s;
  File location = new File(filePath);

  OutputStream print = new FileOutputStream(location);
  print.write(contents.getBytes());
  print.close();

Basically I want to put the file in the same directory as the program which is making it rather than within it so I cape up with the path "filePath" however whenever the OutputStream tries to write to it I get a security exception. Is their a known fix or better way to do this?

Michael Zakariaie
  • 165
  • 1
  • 1
  • 10
  • it depends upon authority of user, whether he/she have right permission of File R W – manikant gautam Mar 23 '16 at 07:44
  • Better to use `user.home` or `java.io.tmpdir` to save your file. The executing user should always have access to those. Beware that the OS might decide to clean up `java.io.tmpdir` and the file might get deleted. – Ralf Mar 23 '16 at 07:55
  • does user.home make the file in the desktop? @Ralf – Michael Zakariaie Mar 23 '16 at 08:05
  • or can i add File.seperator + "desktop" to it without dealing with security? – Michael Zakariaie Mar 23 '16 at 08:06
  • `user.home` is the home directory of your user. The concept of a home directory is portable across operating systems. While the concept of the "Desktop" exists in all(?) mainstream OSs and the folder is called `Desktop` most of the time, there is no guarantee that this folder exists. But nothing prevents you from testing whether it exists and whether the location is writable. If the test is positive, then save the file there. If not, then fall back to the user's home directory. – Ralf Mar 23 '16 at 08:59

0 Answers0