How to use a ~ (relative directory) in my java class?
I create a temporary file on the server, that someone uploads. While I was testing, It was fine to use the complete home directory path in my machine. But now that I have to deploy it to a serv开发者_如何转开发er, I tried using a ~, but I get a
java.io.FileNotFoundException: ~/test/csvFile.csv (No such file or directory)
how do I use something analogous to a ~ ie short for the home directory in *nix. I am using the java.io.File package.
Thanks.
System.getProperty("user.home") ; //will return the path to user home directory.
If i understood it correctly you are looking for how to get user dir path
If you really want a temporary file, you should stop caring about which directory this ends up in (usually) and instead use
File tempFile = File.createTempFile("csvFile", ".csv");
~
is deciphered by Unix shell, not by your program. To get the same effect, get the value of "HOME"
System.getenv("HOME")
The user.home
system property points to the current user's home directory.
However, you may want to consider File.createTempFile()
instead for temporary files.
~
is not a relative directory. .
is a relative directory. ~
corresponds to the value of the system property user.home
. However java.io.File
already contains several methods for temporary files: use those.
精彩评论