saving . . . saved I am not getting the desired output as shown in the lecture neither for error type nor for found file type has been deleted. I am not getting the desired output as shown in the lecture neither for error type nor for found file type has been hidden .
I am not getting the desired output as shown in the lecture neither for error type nor for found file type
Title
Question
Madam/Sir,
Please go through the following Google Drive links to rectify my problem which I haven't found during regular lectures:-

https://drive.google.com/file/d/14VPcA6vWHudZNm6D7TUVYtQjMn13GJLx/view?usp=sharing

https://drive.google.com/file/d/1FV4fnZ6uNGzaXNOdPdwMQTrgly87wMgh/view?usp=sharing

https://drive.google.com/file/d/1AS8xZhBR0j7t50TyfQ5XNP7Fa732_fkR/view?usp=sharing



Java Exception-Handling 08-09 min 30-40 sec 22-06-22, 1:38 p.m. sc256356

Answers:

Make sure to import packages as highlighted below.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class MarksFile
{
public static void main(String[] args)
{
FileReader fr=null;
try
{
fr=new FileReader("C://Users/Shubhankar/Demo/Gemini.txt");
}
catch (FileNotFoundException e)
{
//TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
System.out.println("Inside finally block");
try
{
fr.close();
}
catch (IOException  e)
{
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}



26-06-22, 11:34 a.m. pankajap@cse.iitb.ac.in
Why we need fr.close() also why it's with exception problem that we rectified with try catch block.
If say a file is accessed with this fr object the why I can't see anything special on command prompt just program get executed and finally block executes. Physically what happened when we accessed the file using FileMaker object fr.
26-06-22, 12:37 p.m. sc256356
FileReader object is created to access file and make read/write operations on the accessed file. FileReader object is created on the heap space of memory. By invoking close() method on the FileReader object we close the stream and release all the system resources linked with it. If don't close the FileReader object then it leads to unused object in the heap space. Thus, it causes memory leak issue.

FileReader object is created in try-catch block because if the file doesn't exist then this exception will be handled.

As you have not written any business logic on creation of file reader object, you don't see anything on the console.
27-06-22, 10:57 a.m. pankajap@cse.iitb.ac.in

Login to add comment


Log-in to answer to this question.