我不知道爲什麼,但outStream = new FileOutputStream(file)
和inStream = new FileInputStream(new File(file1.getName()))
拋出異常。我不知道該怎麼做。不正確的文件路徑
這裏是這樣的代碼:
File tempf = new File(cmds[1]); //cmds is a String with filename cmds[1] and pathname cmds[2] where to move the file
File tempw = new File(cmds[2]);
if(!tempf.isAbsolute() || !tempw.isAbsolute()){//here i make paths absolute
tempf = new File(tempf.getAbsolutePath());
tempw = new File(tempw.getAbsolutePath());
}
String from = cmds[1];
String where = cmds[2];
File file1 = tempf;
File file2 = new File (tempw.toString() + "/" + new File(cmds[1]).getName());
InputStream inStream = null;
OutputStream outStream = null;
try {
inStream = new FileInputStream(new File(file1.getName())); //throws an exception
outStream = new FileOutputStream(file2); //throws an exception too
byte[] buffer = new byte[16384];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
if (inStream != null)
inStream.close();
if (outStream != null)
outStream.close();
file1.delete();
} catch (IOException e) {
System.err.println("permission denied");
}
} else {
System.err.println("incorrect syntax");
}
continue;
}
看起來一切都應該工作正常,但事實並非如此。我越來越
java.io.FileNotFoundException: C:\Users\Maxim\IdeaProjects\Testing\OneDrive\1234.txt
但是,正如我所看到的是錯誤的道路。真正的路徑是C:\Users\Maxim\OneDrive
UPD!發現問題是getAbsolutePath()
返回項目所在的路徑,但它不是我需要的路徑。我需要C:\Users\Maxim\OneDrive
但它返回C:\Users\Maxim\IdeaProjects\Testing\OneDrive
但是! .../Testng
沒有OneDrive!
顯示EXCEPTION !!! – Parth 2014-10-06 15:24:08
關於例外情況,尤其是標準庫的情況,最重要的是他們會告訴你爲什麼會發生這種情況。 – 2014-10-06 15:24:17