
There is a file named Name.txt that contains information related to appliances.I want to read the contents of this file and I used the code below:
import java.io.*; public class Appliance { public static void main(String args[]) { try { File file=new File("Name.txt"); FileReader f=new FileReader(file); int ch; while((ch=f.read()) { System.out.print((char)ch); } } catch(FileNotFoundException fnfe) { System.out.println("Exception: "+fnfe.toString()); } catch(IOException ioe) { System.out.println("Exception: "+ioe.toString()); } } } However the preceding code results in an error.Identify and document the reasons for the error and make the code free of compilation errors.

Java Read the content of the file.
import java.io.*;
public class Appliance {
public static void main(String args[]) {
try {
File file=new File("Name.txt");
FileReader f=new FileReader(file);
int ch;
while ((ch = f.read()) != -1){
System.out.print((char)ch);
}
}
catch(FileNotFoundException fnfe) {
System.out.println("Exception: "+fnfe.toString());
}
catch(IOException ioe) {
System.out.println("Exception: "+ioe.toString());
}
}
}

Java Read content of File
import java.io.*;
public class Appliance {
public static void main(String args[]) {
try {
File file=new File("Name.txt");
FileReader f=new FileReader(file);
int ch;
while ((ch = f.read()) != -1){
System.out.print((char)ch);
}
}
catch(FileNotFoundException fnfe) {
System.out.println("Exception: "+fnfe.toString());
}
catch(IOException ioe) {
System.out.println("Exception: "+ioe.toString());
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.