Java是否获取我的项目/bin/文件夹中的文件路径?

Java是否获取我的项目/bin/文件夹中的文件路径?,java,bin,Java,Bin,您好,我非常感谢您的帮助,我基本上需要获取位于我的/bin/path中的文件路径您的项目的本地路径可以使用 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)

您好,我非常感谢您的帮助,我基本上需要获取位于我的
/bin/path
中的文件路径您的项目的本地路径可以使用

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();
}
如果正在运行的jar是
c:\workdir\myproject\bin\myproject.jar
那么
System.getProperty(“user.dir”)
将返回
c:\workdir\myproject\bin

下面是如何在代码中使用它

System.getProperty("user.dir");

可以使用找到项目的本地路径

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();
}
如果正在运行的jar是
c:\workdir\myproject\bin\myproject.jar
那么
System.getProperty(“user.dir”)
将返回
c:\workdir\myproject\bin

下面是如何在代码中使用它

System.getProperty("user.dir");

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

项目;性质;java构建路径;来源;添加文件夹-选择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();
}

或者您可以从C:/to路径定义完整的系统路径。

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

项目;性质;java构建路径;来源;添加文件夹-选择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();
}

或者您可以定义从C:/to路径的完整系统路径。

为什么可以让to路径正常,而不是from?Does
path from=Paths.get(“C:\\bin\\s.txt”)不工作?为什么会这样?我试图从我的项目中获取该文件,不确定它在Java中的名称,但在大多数其他语言中它被称为引用。我需要获取位于bin文件夹中的文件,你知道有bin,src。希望这能解决问题。你怎么能得到正确的路径而不是from?Does
path from=Paths.get(“C:\\bin\\s.txt”)不工作?为什么会这样?我试图从我的项目中获取该文件,不确定它在Java中的名称,但在大多数其他语言中它被称为引用。我需要获取位于bin文件夹中的文件,你知道有bin,src。希望这能解决问题。是的,但我需要获取user.dir/bin aka user.dir/bin/s.txt中的文件,如果这使senseNevermind我找到了:String hi=System.getProperty(“user.dir”);字符串sex=“/bin/s”;Path FROM=Path.get(嗨,sex);Path TO=Path.get(hi,“/s.txt”);系统输出打印项次(至);尝试{Files.copy(FROM,TO);}catch(IOException e){e.printStackTrace();}是的,但是我需要获取user.dir/bin aka user.dir/bin/s.txt中的文件,如果这使sensenneverind我发现了它:String hi=System.getProperty(“user.dir”);字符串sex=“/bin/s”;Path FROM=Path.get(嗨,sex);Path TO=Path.get(hi,“/s.txt”);系统输出打印项次(至);尝试{Files.copy(FROM,TO);}catch(IOException e){e.printStackTrace();}