LoginSignup
21
19

More than 5 years have passed since last update.

java.io.tmpdirのOSによる値の差異について

Last updated at Posted at 2014-07-18

概要

一時ファイル等を作成する際に利用するシステムプロパティjava,io.tmpdir
実行するOSによって、値に差異がありました。
本番サーバがLinux。開発環境はWindowsなんて場合に起きやすいかも。
気が付きにくい割に結構、致命的だったので調査結果を記載。

サンプル

具体的なプログラムを下記に記載。
こんな感じでコーディングしてました。

sample
import java.io.File;

public class TempDir {
    public static void main(String[] args) {
        // 一時ディレクトリに作成したファイルのパスを取得する
        String tempDir = System.getProperty("java.io.tmpdir") + "mydir" + File.separator + "filename.txt";

        // パスを表示
        System.out.println(System.getProperty("java.io.tmpdir"));
        System.out.println(tempDir);
    }
}

この出力結果がOSによって差異があるのです。
Windwos7
C:\Users\username\AppData\Local\Temp\
C:\Users\username\AppData\Local\Temp\mydir\filename.txt

Linux(CentOS)
/tmp
/tmpmydir/filename.txt

おおっと
java.io.tmpdirの値の末尾の区切り文字の有無の違いがありますね。
どちらかの環境のみでテストをしていると気が付きません…

endsWith( File.separator)で末尾の区切り文字の有無を確認して差異を吸収してあげることで対処。

他のOSに関してはこちらの記事参照。
参照:java.io.tmpdir Inconsitency

21
19
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
21
19