์ ํต์ ์ผ๋ก ์์์ด ์ ๋๋ก ๋ซํ์ ๋ณด์ฅํ๋ ์๋จ์ผ๋ก try-finally๊ฐ ์ฐ์๋ค. ํ์ง๋ง ์๋์ 2๊ฐ์ง ๋ฌธ์ ์ ์ผ๋ก ์ธํด try-with-resources์ ์ฌ์ฉ์ด ๊ถ์ฅ๋๋ค.
- ๋ณต์์ ์์์ ์ฒ๋ฆฌํ๊ฒ ๋๋ฉด ์ฝ๋๊ฐ ์ง์ ๋ถํด์ง๋ค.
- ์คํ ์ถ์ ๋ด์ญ์ ์ด์ ์ ์์ธ์ ๊ดํ ์ ๋ณด๋ ๋จ์ง ์๊ฒ ๋์ด, ์ค์ ์์คํ ์์์ ๋๋ฒ๊น ์ ๋ชน์ ์ด๋ ต๊ฒ ํ๋ค.
try-with-resources์ ์ฅ์ ๋ค์ ํตํด ํด๋น ๊ธฐ๋ฅ์ ์์๋ณด์.
์ฅ์ 1. ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค.
try-finally์ ๊ธฐ๋ณธ๊ตฌ์กฐ๋ ์๋์ ๊ฐ๋ค.
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
๋ง์ฝ try-finally๋ก ์ฌ๋ฌ ๊ฐ์ ๋ฆฌ์์ค๋ฅผ ๋ค๋ฃจ์ด์ผ ํ๋ค๋ฉด ์๋์ ๊ฐ์ ๊ฒ์ด๋ค.
static void copy(String src, String dst) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
} finally {
out.close();
}
} finally {
in.close();
}
}
ํน์๋ ์๋์ ๊ฐ์ด ์๊ฐํ์๋ค๋ฉด, ์ด๋ ๊ฒ๋ ํ๋ฉด ์ ๋๋ค. ๋ง์ฝ in.close()์์ ์๋ฌ๊ฐ ๋๋ฉด out.close()๊ฐ ์คํ์กฐ์ฐจ ๋์ง ์๋ ๋ฌธ์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
static void copy(String src, String dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
try {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
} finally {
out.close();
in.close();
}
}
try-with-resources๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์๋์ ๊ฐ๋ค. ์ด๋ close()๋ฅผ ์ง์ ํ์ง ์์๋ ๋๋ค. ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์๋ง์ ํด๋์ค์ ์ธํฐํ์ด์ค๊ฐ ์ด๋ฏธ AutoCloseable์ ๊ตฌํํ๊ฑฐ๋ ํ์ฅํด๋๊ธฐ ๋๋ฌธ์ด๋ค.
static String firstLineOfFile(String path) throws IOException {
try (BufferedReader br = new BufferdReader(
new FileReader(path))) {
return br.readLine();
}
}
๋ณต์์ ์์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค. ์ด๋ in๊ณผ out์ close() ์คํ์ ๋ณด์ฅํ ๋ฟ๋ง ์๋๋ผ try-finally๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ๋ณด๋ค ํจ์ฌ ๊ฐ๊ฒฐํ๋ค.
static void copy(String src, String dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)) {
byte[] buf = new byte[BUFFER_SIZE];
int n;
while((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
}
}
์ฅ์ 2. ์์ธ๋ฅผ ์ก์๋จน์ง ์๋๋ค.
์๋์ ์ฝ๋๊ฐ ์๋ค๊ณ ํ์.
public class BadBufferedReader extends BufferedReader {
public BadBufferedReader(Reader in, int sz) {
super(in, sz);
}
public BadBufferedReader(Reader in) {
super(in);
}
@Override
public String readLine() throws IOException {
throw new CharConversionException();
}
@Override
public void close() throws IOException {
throw new StreamCorruptedException();
}
}
์๋์ฒ๋ผ try-finally๋ฅผ ์ฌ์ฉํ๋ค๋ฉด StreamCorruptedException()๋ง ๋ณด์ธ๋ค. ์ ํํ๋ ๊ฐ์ฅ ๋์ค์ ๋ฐ์ํ ์์ธ๋ง ๋ณด์ธ๋ค. (์ด์ ์ ์์ธ๊ฐ ๋จนํ๋ค๋ผ๊ณ ํํํ ์ ์๊ฒ ๋ค.) ๋ฌผ๋ก ์ด์ ์ ์์ธ๊ฐ ๋ณด์ด๊ฒ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ง๋ง ๊ทธ๋ฌ๋ฉด ์ฝ๋๊ฐ ์ง์ ๋ถํด์ง๋ค.
static String firstLineOfFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
ํ์ง๋ง, try-with-resources๋ฅผ ์ฌ์ฉํ๋ค๋ฉด CharConversionException()์ด ๊ฐ์ฅ ๋จผ์ ๋ณด์ด๊ณ StreamCorruptedException()๋ ๋ณด์ด๊ฒ ๋๋ค. ์ ํํ๋ ์จ๊ฒจ์ง ์ ์์๋ ์์ธ๋ค์ด '์จ๊ฒจ์ก๋ค(suppredssed)'๋ผ๋ ๊ผฌ๋ฆฌํ๋ฅผ ๋ฌ๊ณ ์ถ๋ ฅ๋๋ค.
static String firstLineOfFile(String path) throws IOException {
try (BufferedReader br = new BufferdReader(
new FileReader(path))) {
return br.readLine();
}
}
try-with-resources๋ฅผ ์ฌ์ฉํ๋ฉด์๋ ๋ฌผ๋ก catch์ finally ๋ธ๋ก๋ ๋ชจ๋ ์ฌ์ฉํ ์ ์๋ค. ๊ฒฐ๊ตญ try-with-resources๊ฐ try-finally์ ๋นํด ์ฅ์ ๋ง ๋ ์์ผ๋ฏ๋ก try-with-resources์ ์ฌ์ฉ์ด ๊ถ์ฅ๋๋ค.
ํด๋น ๊ธ์ ๋ฐฑ๊ธฐ์ ๋์ '์ดํํฐ๋ธ ์๋ฐ ์๋ฒฝ ๊ณต๋ต'์ ์๊ฐํ๊ณ ์์ฑํ ๊ฒ์ ๋๋ค.