How to use ~ (relative directory) in my java class?

I am creating a temporary file on a server that someone is uploading. While I was testing, it was good to use the full path to the home directory on my machine. But now that I have to deploy it to the server, I tried to use ~, but I get

java.io.FileNotFoundException: ~/test/csvFile.csv (No such file or directory)

how can I use something similar, for example, short for the home directory in * nix. I am using the java.io.File package.

Thank.

+3
source share
5 answers

If you really need a temporary file, you should stop worrying about which directory it ends in (usually), and use instead

File tempFile = File.createTempFile("csvFile", ".csv");
+3
source
System.getProperty("user.home") ; //will return the path to user home directory.

, , dir

+7

~ Unix, . , "HOME"

System.getenv("HOME")
+3

user.home .

, File.createTempFile() .

+2

~ . . - . ~ user.home. java.io.File : .

0

Source: https://habr.com/ru/post/1771061/


All Articles