1

My need is to open a file (temp file) and write something to it How can I do it so that it would work for all the OS types (at least for unix and windows)

Below is my current code Every time, I want to test something on windows, I will be to toggle between these lines (comment/uncomment)

//File file = new File("C:\\PM_DELETE_CARRIER_TEST_FOLDER\\"+carrier.getCarrierId()+"Carrier_BackUp_Restore.sql");
File file = new File("/app/jakarta-tomcat/logs/Carrier_BackUps/"+carrier.getCarrierId()+"Carrier_BackUp_Restore.sql");
Arun
  • 526
  • 4
  • 13
  • Use relative addressing of directories. Instead of `c:/` use `./`... – Boris Pavlović Sep 18 '14 at 06:41
  • For the separator just use `'/'`. It works on all platforms in Java. – user207421 Sep 18 '14 at 06:47
  • @EJP While most mainstream platforms use or support `/` as a file separator, it's a stretch to say that it holds for "all platforms." The `File.separator` constant (or system property) is the only _truly portable_ solution guaranteed by the language and runtime. – William Price Sep 18 '14 at 07:17

3 Answers3

3

You can make use of the System properties in Java. Say suppose Use "user.home" in your file path so that the file will be placed in the user home directory. You don't have to switch in between

Ex:

File file = new File(System.getProperty("user.home")+File.separator+
"Carrier_BackUp_Restore.sql");

Adding another way by @EJP comment to avoid file separator

File file = new File(System.getProperty("user.home"), "Carrier_BackUp_Restore.sql");
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • You do not need the file.separator property. You can use `File.separator` (See here for the why of this http://stackoverflow.com/a/2417515/461499 ) – Rob Audenaerde Sep 18 '14 at 06:45
  • @RobAu Done, changed. Thank you for tip! – Keerthivasan Sep 18 '14 at 06:47
  • You don't need that either: either use `"/'` or just write `new File(System.getProperty("user.home"), "Carrier_BackUp_Restore.sql")`. – user207421 Sep 18 '14 at 06:48
  • While most mainstream platforms use or support `/` as a file separator, the `File.separator` constant (or system property) is the only _truly portable_ solution guaranteed to work, even on specialized platforms. – William Price Sep 18 '14 at 07:15
1

For a temporary file, you can let the JVM choose a suitable path for you: see File.createTempFile.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
0

"My need is to open a file (temp file) and write something to it" . Why not use Files.createTempFile() but remember the file is not automatically deleted , You can use deleteOnExit() or open it via DELETE_ON_CLOSE options. If you want to create the file manually then use Paths

Paths.get("foo", "somelocation").toFile();
sol4me
  • 15,233
  • 5
  • 34
  • 34
  • I tried this as well and it does create a tmp file. Thanks – Arun Nov 07 '14 at 06:57
  • @Arun glad it helps. The accepted answer is using java 6 style and the answer mentioned by me uses Java 7. Java 7 added quit nice features to work with Files. Take a look on them to make your code more better – sol4me Nov 07 '14 at 14:11