我爲工作文件,並試圖meke多個文件和我得到的例外是:系統無法找到路徑,** IO異常**
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.create.CreatingFiles.create(CreatingFiles.java:25)
at com.create.CreatingFiles.main(CreatingFiles.java:36)
和代碼是:
File file = new File("F://fileIO");
StringBuffer buffer = null;
File newFile;
try {
if (!file.exists()) {
file.mkdir();
buffer = new StringBuffer(file.getAbsolutePath().toString());
} else {
System.out.println("DIRECTORY EXISTS");
buffer = new StringBuffer(file.getAbsolutePath().toString());
}
for (int i = 0; i < 10; i++) {
newFile = new File(buffer.append("/new File").append(i)
.append(".txt").toString()); //ERROR
if (!newFile.exists()) {
newFile.createNewFile();
System.out.println(newFile.getAbsolutePath());
} else {
System.out.println("FILE EXISTS");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
現在糾正我,如果我錯了,我在想,我需要關閉文件資源,以便我可以重新分配給新文件,但不能關閉它。
或
還有其他的東西導致了這個錯誤?
顯示包含行號的完整堆棧跟蹤。 – Kayaman
'/'字符不需要轉義。這是'\\',需要重複它才能逃脫。您可以嘗試「F:\\ fileIO」 –
@HankD其實我試圖在** fielIO **下創建一個新的文本文件,所以我使用**/fileName **,**/**將提供路徑文件名稱。 是一個錯誤的方法? – emotionlessbananas