GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - BufferedReader close() method
Selected Reading
Java - BufferedReader close() methodDescriptionThe Java BufferedReader Close() method closes the stream and releases any system resources associated with it. After the closing of the stream, read(), ready(), mark(), reset(), or skip() invocation will throw IOException. DeclarationFollowing is the declaration for java.io.BufferedReader.close() method. public void close() ParametersNA Return ValueThis method does not return any value. ExceptionIOException − If an I/O error occurs. AssumptionAssuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs − ABCDE Example - Using close() methodThe following example shows the usage of Java BufferedReader close() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
// open input stream example.txt for reading purpose.
is = new FileInputStream("example.txt");
// create new input stream reader
isr = new InputStreamReader(is);
// create new buffered reader
br = new BufferedReader(isr);
// releases any system resources associated with reader
br.close();
// creates error
br.read();
} catch(IOException e) {
// IO error
System.out.println("The buffered reader is closed. " + e.getMessage());
} finally {
// releases any system resources associated
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − The buffered reader is closed. Stream closed Example - Closing a BufferedReader After Reading from a FileThe following example shows the usage of Java BufferedReader close() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
BufferedReader reader = null;
try {
// Input string
String input = "Hello, World!\nWelcome to the BufferedReader"+
"example.\nThis is line 3.";
// Initialize BufferedReader with StringReader
reader = new BufferedReader(new StringReader(input));
// Read and print the string content line by line
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
} finally {
// Close the BufferedReader to release resources
try {
if (reader != null) {
reader.close();
System.out.println("BufferedReader closed successfully.");
}
} catch (IOException e) {
System.err.println("Error while closing BufferedReader: " +
e.getMessage());
}
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello, World! Welcome to the BufferedReader example. This is line 3. BufferedReader closed successfully. Explanation
Example - Using BufferedReader with try-with-resourcesThe following example shows the usage of Java BufferedReader close() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
// Input string
String input = "Hello, World!\nWelcome to the BufferedReader example.\nThis is line 3.";
String line;
// Using try-with-resources for automatic resource management
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Read and print the string content line by line String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello, World! Welcome to the BufferedReader example. This is line 3. Explanation
All examples demonstrate how to use the close() method, with the last approach being preferred due to its simplicity and automatic resource management. java_io_bufferedreader.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |