Learn Java Programming - InputStreamReader class Tutorial

Daniel Ross
Daniel Ross
12.8 هزار بار بازدید - 9 سال پیش - The InputStreamReader class can serve
The InputStreamReader class can serve multiple purposes, but primarily it is used to convert byte streams to character streams. This tutorial builds on concepts from my Introduction to I/O Tutorial, I highly recommend watching it first. InputStreamReader has several overloaded constructors with each on taking an InputStream (like System.in, or FileInputStream) as the first parameter. This tutorial will utilize the constructor that only takes a single parameter. The InputStreamReader class has a method called close() that will close the stream and releases any system resources associated with it. The close() method should always be called once you are done with the input stream.

In Java 7 a new feature called try-with-resources was introduced. Specifically a resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. How do we know if a class is a resource? Simple, if it implements java.lang.AutoCloseable, the class can be considered a resource. I'll demonstrate how this feature works.
9 سال پیش در تاریخ 1394/09/20 منتشر شده است.
12,867 بـار بازدید شده
... بیشتر