LoginSignup
1
1

More than 5 years have passed since last update.

Javaで~(チルダ)は使えない!

Last updated at Posted at 2015-12-11

頭を抱えました.
検索クエリが~だとあまりヒットしないかもと思ったので(チルダ)とつけておきました.
決してふりがなのつもりではないので悪しからず.

実行環境

Mac 10.11.1
Java 7

だめなやつ

dirっていうディレクトリつくるとして..

createDir
    File file = new File("~/dir");
    if(file.exists()){
        System.out.println("dirあるで")
    }
    else{
        file.mkdir();
    }

これじゃできません.
Javaだと~は使えないらしいですね.
つまりはそもそも~なんてものがないからfile(~/dir)があるかないかの前に,~がないんですね.

いいやつ

dirっていうディレクトリつくるとして..

createDir
    String homeDir = System.getenv("HOME");
    File file = new File(homeDir + "/dir");
    if(file.exists()){
        System.out.println("dirあるで")
    }
    else{
        file.mkdir();
    }

こうすると無事に/home/shu920921/dirができます!

System.getenv("HOME") = /home/shu920921でした

1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1