Closeable 구현 또는 AutoCloseable 구현 수있는 방법을 이해할 수

나는 자바를 배우는 과정에서 그리고 난 온 좋은 설명 찾을 수 없습니다 implements Closeableimplements AutoCloseable인터페이스를.

를 구현할 때 interface CloseableEclipse 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 been created");

    pw.println("file has been created");

}

@Override
public void close() throws IOException {


}


답변

인터페이스에 익숙하지 않은 것 같습니다. 게시 한 코드에서 AutoCloseable.

닫아야하는 파일 또는 기타 리소스를 처리하는 자체를 구현 Closeable하거나 구현 AutoCloseable하려는 경우 에만 (또는해야합니다) PrintWriter.

구현에서 pw.close(). finally 블록에서이 작업을 수행해야합니다.

PrintWriter pw = null;
try {
   File file = new File("C:\\test.txt");
   pw = new PrintWriter(file);
} catch (IOException e) {
   System.out.println("bad things happen");
} finally {
   if (pw != null) {
      try {
         pw.close();
      } catch (IOException e) {
      }
   }
}

위의 코드는 Java 6과 관련이 있습니다. Java 7에서는 더 우아하게 수행 할 수 있습니다 ( 이 답변 참조 ).


답변

AutoCloseable(Java 7에 도입 됨) try-with-resources 관용구 를 사용할 수 있습니다 .

public class MyResource implements AutoCloseable {

    public void close() throws Exception {
        System.out.println("Closing!");
    }

}

이제 다음과 같이 말할 수 있습니다.

try (MyResource res = new MyResource()) {
    // use resource here
}

JVM이 close()자동으로 호출 합니다.

Closeable 이전 인터페이스입니다. 몇 가지 이유이전 버전과의 호환성을 유지하기 위해 언어 디자이너는 별도의 버전을 만들기로 결정했습니다. 이를 통해 모든 Closeable클래스 (예 : throwing IOException)를 try-with-resources에서 사용할 수있을뿐만 아니라에서보다 일반적인 검사 예외를 throw 할 수도 있습니다 close().

의심 스러우면을 사용하면 AutoCloseable클래스의 사용자가 감사 할 것입니다.


답변

Closeableextends AutoCloseable, 특히 IO 스트림 전용입니다. Exception 대신 IOException을 throw하고 멱 등성을 발생시키는 반면 AutoCloseable은 이러한 보장을 제공하지 않습니다.

이것은 모두 두 인터페이스의 javadoc에 설명되어 있습니다.

AutoCloseable (또는 Closeable)을 구현하면 클래스를 Java 7에 도입 된 try-with-resources 구조의 리소스로 사용할 수 있습니다. 이렇게하면 닫는 finally 블록을 추가하지 않고도 블록 끝에서 이러한 리소스를 자동으로 닫을 수 있습니다. 자원을 명시 적으로.

클래스는 닫을 수있는 리소스를 나타내지 않으며이 인터페이스를 구현하는 데 전혀 의미가 없습니다. IOTest를 닫을 수 없습니다. 인스턴스 메서드가 없기 때문에 인스턴스화 할 수도 없습니다. 인터페이스를 구현한다는 것은 클래스와 인터페이스간에 is-a 관계 있다는 것을 의미합니다 . 여기에는 그런 관계가 없습니다.


답변

다음은 작은 예입니다.

public class TryWithResource {

    public static void main(String[] args) {
        try (TestMe r = new TestMe()) {
            r.generalTest();
        } catch(Exception e) {
            System.out.println("From Exception Block");
        } finally {
            System.out.println("From Final Block");
        }
    }
}



public class TestMe implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println(" From Close -  AutoCloseable  ");
    }

    public void generalTest() {
        System.out.println(" GeneralTest ");
    }
}

다음은 출력입니다.

GeneralTest
From Close -  AutoCloseable
From Final Block

답변

try-with-resources문.

try-with-resources statementA는 try하나 개 이상의 자원을 선언 문. A resource는 프로그램이 완료된 후 닫아야하는 개체입니다. 은 try-with-resources statement각 리소스가 명령문의 끝에서 닫히도록합니다. 를 구현 java.lang.AutoCloseable하는 모든 객체.java.io.Closeable 리소스로 사용할 수 있습니다.

다음 예제는 파일에서 첫 번째 줄을 읽습니다. 의 인스턴스를 사용 BufferedReader하여 파일에서 데이터를 읽습니다. BufferedReader프로그램이 완료된 후 닫아야하는 리소스입니다.

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

이 예에서 try-with-resources 문에 선언 된 리소스는 BufferedReader입니다. 선언문은 try 키워드 바로 뒤의 괄호 안에 표시됩니다. BufferedReaderJava SE 7 이상에서 클래스 는 인터페이스를 구현합니다 java.lang.AutoCloseable. BufferedReader인스턴스는 try-with-resource 문에서 선언 되기 때문에 try 문이 정상적으로 완료되었는지 또는 갑작스럽게 완료되었는지에 관계없이 닫힙니다 (를 BufferedReader.readLine던지는 메서드의 결과로 IOException).

Java SE 7 이전에는 finally블록을 사용 하여 try 문이 정상적으로 완료되었는지 갑작스럽게 완료되었는지에 관계없이 리소스가 닫혔는지 확인할 수 있습니다 . 다음 예제에서는 문 finally대신 블록을 사용 try-with-resources합니다.

static String readFirstLineFromFileWithFinallyBlock(String path)
                                                     throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(path));
    try {
        return br.readLine();
    } finally {
        if (br != null) br.close();
    }

}

제발 워드 프로세서를 참조하십시오 .


답변

최근에 Java SE 8 Programmer Guide ii Book을 읽었습니다.

나는 사이의 차이에 대해 뭔가 발견 AutoCloseable대를 Closeable.

AutoCloseable인터페이스는 Java 7에서 도입되었습니다. 그 전에라는 또 다른 인터페이스가있었습니다 Closeable. 다음과 같은 예외를 제외하고는 언어 디자이너가 원하는 것과 비슷했습니다.

  • Closeablethrow되는 예외 유형을로 제한합니다 IOException.
  • Closeable 구현이 멱 등성이 있어야합니다.

언어 디자이너는 이전 버전과의 호환성을 강조합니다. 기존 인터페이스를 변경하는 것이 바람직하지 않았기 때문에라는 새 인터페이스를 만들었습니다 AutoCloseable. 이 새로운 인터페이스는보다 덜 엄격합니다 Closeable. Closeable요구 사항을 충족하기
때문에AutoCloseableAutoCloseable 후자가 도입되었을 때 구현되기 시작했습니다 .