好的,所以我只是在编写一个快速课程,我尝试使用资源尝试而不是 try-catch-finally(讨厌这样做)方法,并且我不断收到错误“Illegal start of type”。然后我转向了关于它的 Java 教程部分:http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
它表明您可以在括号中分配一个新变量。我不确定发生了什么。
private static final class EncryptedWriter {
private final Path filePath;
private FileOutputStream outputStream;
private FileInputStream inputStream;
public EncryptedWriter(Path filePath) {
if (filePath == null) {
this.filePath = Paths.get(EncryptionDriver.RESOURCE_FOLDER.toString(), "Encrypted.dat");
} else {
this.filePath = filePath;
}
}
public void write(byte[] data) {
try (this.outputStream = new FileOutputStream(this.filePath.toFile())){
} catch (FileNotFoundException ex) {
Logger.getLogger(EncryptionDriver.class.getName()).log(Level.SEVERE, null, ex);
}
}
}