Since I didn't directly assign InputStreamReader to variable, I don't know how I can close it. I thought InputStreamReader is wrapped in BufferedReader, it would be closed when I close BufferedReader. Any help?
package com.codegym.task.task13.task1318;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.FileInputStream;
/*
Reading a file
1. Read a file name from the console.
2. Display the contents of the file in the console (on the screen).
3. Don't forget to free up resources. Close the streams for file input and keyboard input.
Requirements:
1. The program must read the file name from the console.
2. The program must display the contents of the file.
3. You must close the file input stream (FileInputStream).
4. The BufferedReader must also be closed.
*/
public class Solution {
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
BufferedReader fileIn = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
String line;
while ((line = fileIn.readLine()) != null){
System.out.println(line);
}
fileIn.close();
reader.close();
}
}