Java中的相对路径 [英] relative path in Java

查看:352
本文介绍了Java中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序必须使用驻留在另一个目录中的某些文件。现在我正在使用绝对路径来指定文件的位置。但是我知道这些文件的相对位置与我的程序所在的位置相比保持不变。如何在Java中使用相对路径来指定这些文件的位置?



例如,我的程序驻留在

  / home / username / project / src / com / xyz / 

..目标文件在..

  / home / username / project / secure / 
'/ code>


解决方案

您电流路径。

  currentPath = / home / username / project / src / com / xyz /; 

/ home / username / project / secure /文件夹的相对路径为

  relativePath = ../../../secure; 


My program has to use certain files that reside in another directory. Right now I am using absolute path to specify the location of the files. But I know that the relative position of these files will remain the same compared to where my program is residing. How I can use relative path in Java to specify the location of these files?

For example my program is residing in

/home/username/project/src/com/xyz/

..and the target files are in..

/home/username/project/secure/

解决方案

Your current path.

 currentPath= /home/username/project/src/com/xyz/;

Relative path to "/home/username/project/secure/" folder is

relativePath= ../../../secure;

这篇关于Java中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆