java里dir是什么意思_关于文件系统:为什么user.dir系统属性在Java中工作?
我讀過的幾乎每篇文章都告訴我,你不能用Java創建chdir。 這個問題的公認答案說你不能用Java做到這一點。
但是,這里有一些我嘗試過的東西:
geo@codebox:~$ java -version
java version"1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)
這是我正在使用的測試類:
import java.io.*;
public class Ch {
public static void main(String[] args) {
System.out.println(new File(".").getAbsolutePath());
System.setProperty("user.dir","/media");
System.out.println(new File(".").getAbsolutePath());
}
}
geo@codebox:~$ pwd
/home/geo
geo@codebox:~$ java Ch
/home/geo/.
/media/.
請解釋為什么這有效。 從現在開始我可以使用它并期望它在所有平臺上以相同的方式工作嗎?
僅僅因為new File(".")給出了所需的答案并不意味著它正在做你想要的。
例如,嘗試:
new FileOutputStream("foo.txt").close();
那最終會在哪里?在我的Windows框中,即使new File(".").getAbsolutePath()基于user.dir移動,也始終在原始工作目錄中創建foo.txt。設置user.dir使得new File(".")不引用當前工作目錄只會讓人感到麻煩。
我沒有測試:)。 當我看到更改user.dir屬性時,我停止了File類。
那就是......所以......所以......錯了。:-)
引用:
The user.dir property is set at VM startup to be the working directory. You should not change this property or set it on the command-line. If you do, then you will see some inconsistent behaviour as there places in the implementation that assumes that the user.dir is the working directory and that it doesn't change during the lifetime of the VM.
討論就在這里
File.getAbsoluteFile()只是查看user.dir系統屬性,它是VM啟動時進程工作目錄的副本。
更好的測試可能是檢查進程的工作目錄是否實際發生了變化。如何做到這一點因平臺而異,但在Linux上你可以這樣:
$ ?ls -l /proc/18037/cwd
lrwxrwxrwx 1 laurence laurence 0 2009-08-05 11:16 /proc/18037/cwd -> /home/laurence/
其中"18037"是該過程的pid。如果你這樣做,我相信你會發現當你更新user.dir時,進程的工作目錄實際上沒有改變。
總結
以上是生活随笔為你收集整理的java里dir是什么意思_关于文件系统:为什么user.dir系统属性在Java中工作?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cpqa1000.exe是安全进程吗 c
- 下一篇: w7系统计算机e盘无法打开,Win7电脑