`
luogl
  • 浏览: 17487 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

java 修改当前目录(current directory)

阅读更多

A recurring question in the Sun Java Forum groups is, "How do I change the current directory of my Java application?" The correct answer is that you don't! Java applications don't use the metaphor of 'current directory' in the same way native Unix and Windows applications do.
A native Unix or Windows application typically by default points at the current directory of the command shell where the program is started, and if you do an fopen("x.txt","w") your file will end up in that directory. You can then call chdir(..) to change the current directory of the application. The metaphor is that the application is like a user at the command line, cd'ing around and accessing files.

On the other hand, when you start a Java application with 'java appname', it is system dependent what the current directory is and you can't change the 'current directory' from a Java application, and the application as user metaphor does not apply.

To understand the Java solution, it is helpful to identify is the real requirement. The requirement is not to change to a new current directory just because you want to. The real requirement is to allow a program to get at files in the file system relative to the current directory. In other words, make it easy to access files in a given location without an absolute path prefix.

In Java, you use File objects to construct a relative view of the file system. Two of the constructors for the File object take a 'parent' argument that specifies a parent path that is prefixed to the path of the file itself to create the full abstract path to the file. What you do is, create a File object with the path that represents your current directory and then create all your file objects using that File object as the parent. Voila, a current directory. If you want to change the current directory, create a new File object with the desired path and use it as the parent. See the SDK documentation, File object for the specifics of the constructors.

The main catch here is that you have to specify the parent somehow, either by knowing it outright, specifying it thru some user input or figuring it out from the application environment. If you know it outright, you can hardcode it or store it as an application property or something similar. The user input could be a command line argument or some run-time input method, such as a JFileChooser object.

On some systems you can read a system property called 'user.dir' to get the path that the application was started in. This property is system dependent as to what it means or whether it exists at all, but in Unix and Windows it returns the directory the command shell was in when the java application was started. On Windows and Unix, "user.dir" is a READ ONLY property, and you can't change the 'current directory' by trying to set the "user.dir" property. You read system properties with the System.getProperty(String) method.

David Howard
参考原文:http://www.devx.com/tips/Tip/13804


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kingswood/archive/2009/12/09/4969754.aspx

分享到:
评论

相关推荐

    Java 实例 - 查看当前工作目录源代码-详细教程.zip

    2. **获取当前工作目录**:在Java中,我们可以使用`java.io.File`类的静态方法`currentDirectory()`来获取当前工作目录。这个方法返回一个`File`对象,表示当前的工作目录。例如: ```java File currentDir = File...

    计算机软件-商业源码-222 取得当前文件所在的目录.zip

    C#中,`System.IO.Directory.GetCurrentDirectory()`获取当前工作目录,`System.Reflection.Assembly.GetExecutingAssembly().Location`获取运行时程序集的路径。 6. **Go**: Go语言提供了`os.Getwd()`函数来获取...

    java新手上路

    - Windows系统中,通过修改注册表项`HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\CurrentVersion`来指定默认使用的JDK版本。 - IDE如JBuilder中,可在项目属性设置中指定使用的JDK版本。 3. **...

    程序员面试宝典

    public class CurrentDirectory { public static void main(String[] args) { String currentDir = System.getProperty("user.dir"); System.out.println("Current working directory is: " + currentDir); } ...

    获取 系统 目录.rar

    - **C#**:使用`Environment.GetFolderPath(Environment.SpecialFolder.System)`获取系统目录,`Environment.CurrentDirectory`获取当前目录。 - **JavaScript**:在Node.js环境下,`process.env`对象用于获取环境...

    Java常用工具类(免费)

    删除目录及目录下的文件:`boolean deleteDirectory(String dir)` 删除整个目录及其所有子文件和子目录。 #### 五、反射工具类 [ReflectUtil] 反射是Java中一项强大的特性,ReflectUtil提供了一系列使用反射进行...

    安卓中文件管理器简单例子

    File currentDirectory = Environment.getExternalStorageDirectory(); File[] files = currentDirectory.listFiles(); ``` 为了显示这些文件和目录,我们可以创建一个自定义的Adapter,例如`FileListAdapter`,它...

    java 技巧小结荟萃

    - **`-classpath path`**:指定查找 Java 文件的目录。 - **`-d directory`**:指定输出 HTML 文件的目录。 --- #### 调试器 - jdb.exe jdb.exe 是 Java 提供的一个强大的命令行调试工具,适用于本地和远程调试。...

    Java程序设计课程设计报告音乐播放器.doc

    9. **文件路径管理**: `currentDirectory` 变量用于存储当前目录,方便用户打开同一目录下的其他音乐文件。 通过这个Java音乐播放器的实现,学生可以学习到Java的GUI编程、事件处理、多媒体处理以及对象和类的设计...

    给“回收站,我的电脑,我的文档”改名 、改图标

    MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons`或`HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons`,根据你要更改的是所有用户的设置还是当前用户...

    ftp4j-1.3.1下载

    String dir = client.currentDirectory(); 改变当前文件夹 client.changeDirectory(newPath); //绝对路径 //client.changeDirectory("/an/absolute/one"); //相对路径 //client.changeDirectory("relative"); //...

    超级有影响力霸气的Java面试题大全文档

    1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面。抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节。抽象包括两个方面,一是过程抽象,二是...

    Matlab_for_Linux安装过程.txt

    - 需要确保当前目录不是CD-ROM,而是目标安装目录。 5. **处理多张CD-ROM**: - 如果安装过程中需要使用多张CD-ROM(如CD1、CD2、CD3),按照顺序依次挂载并执行相应的操作。 6. **配置许可文件**: - 修改`...

    迅雷在线二笔

    描述中提到了一个具体的任务:在一个包含100个文件的目录中,将所有文件名按特定规则修改,并创建一个新的名为`file.txt`的文件。这种需求常见于数据清洗或自动化脚本中,涉及文件系统操作和文本处理。 **解决方案*...

    Lucene4.X实战类baidu搜索的大型文档海量搜索系统-16.Lucene高级进阶2 共4页.pptx

    课程中还提供了多个实战项目,包括创建索引(CreateIndexFile.java)、删除索引(DelDocIndexFile.java)、更新索引(UpdateDocIndexFile.java)和搜索索引(SearchIndexFile.java)的Java代码示例,以及实现分页...

    Git命令速查表,英文高清PDF

    - **解释**: 使用此命令可以查看当前工作目录与最后一次提交之间的差异,包括新增加的文件、被修改的文件以及被删除的文件等。 - **查看未暂存文件的更改(Show the changes to files not yet staged)** - **命令**...

    代码格式化工具

    - Working Directory:设置为源代码目录,这样AStyle就能找到所有相关的头文件。 - Input:选择“Selected File” - Output:选择“None” 保存这个命令后,你就可以在Source Insight中使用它了。选中要格式化的...

Global site tag (gtag.js) - Google Analytics