如何在Java程序中更改当前工作目录?我能找到的关于这个问题的一切声称你根本做不到,但我不相信那是真的.
我有一段代码,它使用来自通常启动的目录的硬编码相对文件路径打开文件,我只是希望能够在不同的Java程序中使用该代码,而无需从内部启动它一个特定的目录.看起来你应该只能调用System.setProperty( "user.dir", "/path/to/dir" ),但据我所知,调用该行只是默默地失败并且什么都不做.
我会理解,如果Java不允许你这样做,如果它不是因为它允许你获得当前的工作目录,甚至允许你使用相对文件路径打开文件....
我正在处理一个应用程序,有一个关于从java应用程序运行shell命令的问题.这是代码:
public String execRuntime(String cmd) {
Process proc = null;
int inBuffer, errBuffer;
int result = 0;
StringBuffer outputReport = new StringBuffer();
StringBuffer errorBuffer = new StringBuffer();
try {
proc = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
return "";
}
try {
response.status = 1;
result = proc.waitFor();
} catch (InterruptedException e) {
return "";
}
if (proc != null && null != proc.getInputStream()) {
InputStream is = proc.getInputStream();
InputStream es = proc.getErrorStream();
OutputStream os = proc.getOutputStream();
try {
while …Run Code Online (Sandbox Code Playgroud)