我嘗試讀取文件並獲取FileNotFoundExeption。file.setReadable(true)後文件不可讀
File file = new File("News.out");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
try{
in.readObject();
}
我檢查文件確實存在於目錄中,並檢查文件的「可讀」屬性。
然後我說的「讀」和「寫」的屬性
file.setReadable(true);
file.setWritable(true);
System.out.println(file.canRead());
System.out.println(file.canWrite());
的程序化設置和日誌假的,假的了。
這可能是什麼原因?
編輯:
我試圖JSR 203,並使用此代碼:
Path path = FileSystems.getDefault().getPath(filename);
try(
final InputStream in = Files.newInputStream(path);
) {
ObjectInputStream objectInputStream = new ObjectInputStream(in);
newsStorage.setEntities((ArrayList<News>) objectInputStream.readObject());
} catch (NoSuchFileException e) {
createFile(path, filename);
handleException(e);
}
而且CREATEFILE()方法:沒有創建
private void createFile(Path path, String string) {
try {
Files.newOutputStream(path, StandardOpenOption.CREATE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
文件。
我是否理解正確,那
Files.newOutputStream(path, StandardOpenOption.CREATE);
應該創建一個文件?
你如何運行你的應用程序? –
@Ortomala Lokni它是Eclipse RCP應用程序,我使用Run Configuration從Eclipse IDE運行它。 –
打印'System.getProperty(「user.dir」))'並檢查'News.out'是否在裏面。 –