Filereader

FileReader The FileReader class in Java is used to read character-based data from files. It is part of the java.io package and is ideal for reading text files. Basic Example import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { try { FileReader reader = new FileReader("example.txt"); int character; while ((character = reader.read()) != -1) { System.out.print((char) character); } reader.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } Best Practices Always close the FileReader to avoid memory leaks. Wrap with BufferedReader for efficient reading of large files. Use try-with-resources for automatic closing. import java.io.*; public class Main { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } 🔗 Related Notes FileWriter Exception Handlers Concurrent Programming

May 8, 2025 · 1 min · Rohan

Filewriter

FileWriter The FileWriter class in Java is used to write character-based data to files. It is part of the java.io package and is generally used for writing text files. Basic Example import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, FileWriter!"); writer.close(); System.out.println("Successfully written to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } Appending to a File Use the overloaded constructor with true to append data. ...

May 8, 2025 · 1 min · Rohan

Input Output Statements

🖨️ Input/Output Statements in Java Java provides various mechanisms to accept input from the user and display output to the screen, typically using classes from the java.io and java.util packages. 📥 Input in Java The most common way to accept user input is through the Scanner class. 🔹 Example Using Scanner import java.util.Scanner; public class InputExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Hello, " + name); } } 📤 Output in Java Java uses System.out.println() and System.out.print() for output. ...

May 8, 2025 · 1 min · Rohan