6
ใช้ปิดได้หรือใช้ AutoCloseable
ฉันอยู่ระหว่างการเรียนรู้ Java และฉันไม่พบคำอธิบายที่ดีเกี่ยวimplements Closeableกับimplements AutoCloseableอินเทอร์เฟซและอินเทอร์เฟซ เมื่อผมดำเนินการinterface Closeable, Eclipse IDE public void close() throws IOExceptionของฉันสร้างวิธีการ ฉันสามารถปิดสตรีมโดยpw.close();ไม่ใช้อินเทอร์เฟซได้ แต่ฉันไม่เข้าใจว่าฉันจะใช้close()วิธีนี้โดยใช้อินเทอร์เฟซได้อย่างไร และจุดประสงค์ของอินเทอร์เฟซนี้คืออะไร? นอกจากนี้ฉันต้องการทราบ: ฉันจะตรวจสอบได้อย่างไรว่าIOstreamถูกปิดจริง ๆ หรือไม่? ฉันใช้รหัสพื้นฐานด้านล่าง import java.io.*; public class IOtest implements AutoCloseable { public static void main(String[] args) throws IOException { File file = new File("C:\\test.txt"); PrintWriter pw = new PrintWriter(file); System.out.println("file has …