我一直在查看代码,并且已经看到尝试使用资源。我以前使用过标准的 try-catch 语句,看起来他们做同样的事情。所以我的问题是Try With Resources vs Try-Catch 它们之间有什么区别,哪个更好。
这是资源的尝试:
objects jar = new objects("brand");
objects can= new objects("brand");
try (FileOutputStream outStream = new FileOutputStream("people.bin")){
ObjectOutputStream stream = new ObjectOutputStream(outStream);
stream.writeObject(jar);
stream.writeObject(can);
stream.close();
} catch(FileNotFoundException e) {
System.out.println("sorry it didn't work out");
} catch(IOException f) {
System.out.println("sorry it didn't work out");
}