Telegram Group & Telegram Channel
➡️ Закрытие потоков в собственном блоке try

Раньше я закрывал потоки InputStream и OutputStream следующим образом:

InputStream is = null;
OutputStream os = null;

try {
is = new FileInputStream("application.json");
os = new FileOutputStream("application.log");
} catch (IOException io) {
// Обработка исключения
} finally {
is.close();
os.close();
}


🗣️ Проблема в том, что если первый поток вызовет исключение, то закрытие второго потока никогда не произойдет.

✔️ Правильный способ:

InputStream is = null;
OutputStream os = null;

try {

is = new FileInputStream("../input/fxrates.txt");
os = new FileOutputStream("../output/fxrates.txt");

......

} finally {

try { if (is != null) is.close(); } catch(IOException e) {//closing quietly}
try { if (os != null) os.close(); } catch(IOException e) {//closing quietly}

}


Java Learning 👩‍💻
Please open Telegram to view this post
VIEW IN TELEGRAM
👍15🤔41😁1



tg-me.com/Java_per_month/2735
Create:
Last Update:

➡️ Закрытие потоков в собственном блоке try

Раньше я закрывал потоки InputStream и OutputStream следующим образом:

InputStream is = null;
OutputStream os = null;

try {
is = new FileInputStream("application.json");
os = new FileOutputStream("application.log");
} catch (IOException io) {
// Обработка исключения
} finally {
is.close();
os.close();
}


🗣️ Проблема в том, что если первый поток вызовет исключение, то закрытие второго потока никогда не произойдет.

✔️ Правильный способ:

InputStream is = null;
OutputStream os = null;

try {

is = new FileInputStream("../input/fxrates.txt");
os = new FileOutputStream("../output/fxrates.txt");

......

} finally {

try { if (is != null) is.close(); } catch(IOException e) {//closing quietly}
try { if (os != null) os.close(); } catch(IOException e) {//closing quietly}

}


Java Learning 👩‍💻

BY Java Learning




Share with your friend now:
tg-me.com/Java_per_month/2735

View MORE
Open in Telegram


Java Learning Telegram | DID YOU KNOW?

Date: |

The Singapore stock market has alternated between positive and negative finishes through the last five trading days since the end of the two-day winning streak in which it had added more than a dozen points or 0.4 percent. The Straits Times Index now sits just above the 3,060-point plateau and it's likely to see a narrow trading range on Monday.

What is Telegram?

Telegram is a cloud-based instant messaging service that has been making rounds as a popular option for those who wish to keep their messages secure. Telegram boasts a collection of different features, but it’s best known for its ability to secure messages and media by encrypting them during transit; this prevents third-parties from snooping on messages easily. Let’s take a look at what Telegram can do and why you might want to use it.

Java Learning from us


Telegram Java Learning
FROM USA