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时,进程的工作目录实际上没有改变。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441941.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Uva - 10935】 Throwing cards away I (既然是I,看来还有Ⅱ、Ⅲ、Ⅳ?)(站队问题队列问题)

题干: Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that …

腾讯 tars java_腾讯TARS开源团队郑苏波:腾讯微服务开发框架的源码剖析

郑苏波:大家下午好!我是腾讯微服务的郑苏波。先做一个框架的介绍,Tars是一个支持多语言内嵌服务治理功能的框槛,能跟DevOps比较好的协同开发。这个框架四大特点:一是对用户透明实现,让业务可以聚焦自己的逻…

【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

题干: An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar …

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

题干: Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specia…

使用java开发应用程序_使用Java中的插件支持开发应用程序

我一直在研究如何开发可以加载插件的应用程序.到目前为止,我已经看到这可以通过定义一个接口来实现,并让插件实现它.但是,我当前的问题是如何在Jars中打包时加载插件.有没有“最好的”方法呢?我正在考虑的当前逻辑是让每个插件和他们的Jar内部寻找实现接口的类.但我…

【POJ - 1995】Raising Modulo Numbers(裸的快速幂)

题干: People are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that t…

软件设计师下午题java_2018上半年软件设计师下午真题(三)

● 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】生成器( Builder)模式的意图是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。图6-1所示为其类图。【Java代码】import java.util.*;class Product {priv…

【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)

题干: Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhil…

java细粒度锁_Java细粒度锁实现的3种方式

最近在工作上碰见了一些高并发的场景需要加锁来保证业务逻辑的正确性,并且要求加锁后性能不能受到太大的影响。初步的想法是通过数据的时间戳,id等关键字来加锁,从而保证不同类型数据处理的并发性。而java自身api提供的锁粒度太大&#xff0c…

【POJ - 1062】【nyoj - 510】昂贵的聘礼 (Dijkstra最短路+思维)

题干: 年轻的探险家来到了一个印第安部落里。在那里他和酋长的女儿相爱了,于是便向酋长去求亲。酋长要他用10000个金币作为聘礼才答应把女儿嫁给他。探险家拿不出这么多金币,便请求酋长降低要求。酋长说:"嗯,如果…

php e notice,PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

举例说明:在Windows环境下:原本在php4.3.0中运行正常的程序,在4.3.1中为何多处报错,大体提示为:Notice:Undefined varialbe:变量名称. 例如有如下的代码: 复制代码 代码如下:if (!$tmp_i) { $tmp_i10; }在4…

【POJ - 3169】 Layout(差分约束+spfa)(当板子记?)

题干&#xff1a; Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < N < 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are nu…

php中pregmatch,php中preg_match的isU代表什么意思

isU是大小写分的意思&#xff0c;这里s还有则不包括换行符而U是反转了匹配数量的值使其不是默认的重复,大概就是这样了个体我们看文章。正则后面的/(.*)/isU &#xff0c;“isU”参数代表什么意思&#xff1f;这是正则中的修正符.i是同时查找大小写字母,s是圆点(.)匹配所有字符…

【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)

题干&#xff1a; In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all…

【HDU - 3499】 Flight (单源最短路+优惠问题)

题干&#xff1a; Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him re…

php能不能用MyBatis,Mybatis与Ibatis的区别

Mybatis与Ibatis的区别:1、Mybatis实现了接口绑定&#xff0c;使用更加方便在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件&#xff0c;而Mybatis实现了DAO接口与xml映射文件的绑定&#xff0c;自动为我们生成接口的具体实现&#xff0c;使用起来变得更加省…

【51Nod - 1001 】 数组中和等于K的数对 (排序+ 尺取)

题干&#xff1a; 给出一个整数K和一个无序数组A&#xff0c;A的元素为N个互不相同的整数&#xff0c;找出数组A中所有和等于K的数对。例如K 8&#xff0c;数组A&#xff1a;{-1,6,5,3,4,2,9,0,8}&#xff0c;所有和等于8的数对包括(-1,9)&#xff0c;(0,8)&#xff0c;(2,6)…

linux php oauth安装,Linux php 扩展安装 mongo ,redis ,soap,imap,pdo_mysql,oauth

安装mongodb 参看文章&#xff1a;2.安装redisyum install gitgit clone git://github.com/owlient/phprediscd phpredis/usr/local/php/bin/phpize./configure --with-php-config/usr/local/php/bin/php-configmake && make install如果上述出现报错&#xff0c;可以尝…

*【CF#510C】Fox And Names (拓扑排序)

题干&#xff1a; Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she…

java质,JAVA分解质因子 - osc_r1gtal48的个人空间 - OSCHINA - 中文开源技术交流社区

/*题目分解质因数(5分)题目内容&#xff1a;每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式&#xff0c;这几个素数就都叫做这个合数的质因数。比如&#xff0c;6可以被分解为2x3&#xff0c;而24可以被分解为2x2x2x3。现在&#xff0c;你的程序要读入一个[2,100…