216k views
0 votes
If a file is opened for reading and the file does not exist,what action is taken?

A)A new file is created.
B)A NULL value is returned.
C)An error message is issued.
D)An EOF value is returned.

User Legesh
by
8.6k points

1 Answer

0 votes

Answer:

C)An error message is issued.

Step-by-step explanation:

If we try to open a file for reading when that file does not exist, we will get an error message.

For example, in Java we will encounter a FileNotFoundException as in the code below:

try {

FileInputStream fis = new FileInputStream("myFile.txt");

DataInputStream dis = new DataInputStream(fis);

BufferedReader br = new BufferedReader(new InputStreamReader(dis));

String str = null;

while ((str = br.readLine()) != null) {

System.err.println(str);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

If the file myFile.txt does not exist we can expect to see an exception stack trace corresponding to FileNotFoundException.

User Shikha
by
6.9k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.