Effective Java


// try-with-resources - the the best way to close resources!



Download 2,19 Mb.
Pdf ko'rish
bet42/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   38   39   40   41   42   43   44   45   ...   341
Bog'liq
Effective Java

// try-with-resources - the the best way to close resources!
static String firstLineOfFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(
new FileReader(path))) {
return br.readLine();
}
}
And here’s how our second example looks using 
try
-with-resources:
// try-with-resources on multiple resources - short and sweet
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);
}
}
Not only are the 
try
-with-resources versions shorter and more readable than the
originals, but they provide far better diagnostics. Consider the 
firstLineOfFile


CHAPTER 2
CREATING AND DESTROYING OBJECTS
36
method. If exceptions are thrown by both the 
readLine
call and the (invisible)
close
, the latter exception is 
suppressed
in favor of the former. In fact, multiple
exceptions may be suppressed in order to preserve the exception that you actually
want to see. These suppressed exceptions are not merely discarded; they are
printed in the stack trace with a notation saying that they were suppressed. You
can also access them programmatically with the 
getSuppressed
method, which
was added to 
Throwable
in Java 7.
You can put catch clauses on 
try
-with-resources statements, just as you can
on regular 
try
-
finally
statements. This allows you to handle exceptions without
sullying your code with another layer of nesting. As a slightly contrived example,
here’s a version our 
firstLineOfFile
method that does not throw exceptions, but
takes a default value to return if it can’t open the file or read from it:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   38   39   40   41   42   43   44   45   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish