Try / Catch 없이 inpustStream close() 호출 방법

2024-11-20


사진: Unsplash 의 Brandon Griggs


1. 방법

 

import com.amazonaws.util.IOUtils;


    public void fileToInputStream(File file) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
			// ...
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(fileInputStream != null){//스트림 객체 안전하게 제거
                IOUtils.closeQuietly(fileInputStream, null);
            }
        }
    }

 


참고 링크 : https://stackoverflow.com/questions/36840545/closing-inputstream-without-a-try-catch-block

 

Closing InputStream without a try/catch block?

I know that an InputStream should be closed. But I have some doubts where and how to do this. According to the documentation on IOUtils.closeQuietly: Unconditionally close an InputStream. Equiv...

stackoverflow.com


메인 이미지 출처 : 사진: UnsplashBrandon Griggs