This program uses a BufferedReader to read the contents of a file and prints them to the console. Here's a breakdown of how it works:
Note that the program uses try-catch blocks to catch any exceptions that might occur while reading the file, such as a FileNotFoundException or an IOException. If an exception occurs, the program prints an error message to the console.
import java.io.File; import java.io.FileReader; import java.io.BufferedReader; public class Buffered_Reader { public static void main(String args[]) { final String file_name = "file.txt"; try { File obj = new File(file_name); if (obj.exists() == false) { System.out.println("File does Not Exist.."); System.exit(0); } String text; FileReader file_read = new FileReader(obj.getAbsoluteFile()); BufferedReader buf_read = new BufferedReader(file_read); //read text from file System.out.println("Content of the file is : "); while ((text = buf_read.readLine()) != null) { System.out.println(text); } buf_read.close(); } catch (Exception e) { System.out.println("Exception : " + e.toString()); } } }
File does Not Exist..
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions