Java在我的project / bin /文件夹中获取文件的路径?

时间:2013-11-25 16:11:14

标签: java bin

Path FROM = Paths.get // need to get my file in my bin folder called s.txt, how would this be done?
Path TO = Paths.get("C:\\Temp\\to.txt");
try {
    Files.copy(FROM, TO);
} catch (IOException e) {
    e.printStackTrace();
}

嗨,我真的很感激帮助,我基本上需要获取位于/bin/path

中的文件的路径

2 个答案:

答案 0 :(得分:1)

可以使用

找到项目的本地路径
System.getProperty("user.dir");

如果您的jar正在运行c:\workdir\myproject\bin\myproject.jar,那么System.getProperty("user.dir");将返回c:\workdir\myproject\bin

以下是如何在代码中使用它...

Path FROM = Paths.get(System.getProperty("user.dir") + "/s.txt");
Path TO = Paths.get("C:\\Temp\\to.txt");
try {
    Files.copy(FROM, TO);
} catch (IOException e) {
    e.printStackTrace();
}

答案 1 :(得分:0)

如果我没记错的话,如果使用eclipse,请转到:

  

项目;性能; java构建路径;资源;添加文件夹 - 选择bin文件夹

然后在你的代码中做

Path TO = Paths.get(getClass().getResource("/bin/s.txt"));

或者您可以从C:/中定义完整的系统路径,就像您的路径一样。