Java FileInputStream
π Java FileInputStream FileInputStream is a byte stream class used to read raw byte data from a file.It is mainly used for binary files like images, videos, or any non-text files, but can also...
π Java FileInputStream FileInputStream is a byte stream class used to read raw byte data from a file.It is mainly used for binary files like images, videos, or any non-text files, but can also...
π» Java I/O Streams In Java, I/O (Input/Output) Streams are used to read data from a source or write data to a destination. InputStream β Read data (input) OutputStream β Write data (output) Streams...
β Java Delete Files Java allows deleting files using: File.delete() (Old, simple method) Files.delete() or Files.deleteIfExists() (Modern, Java NIO) 1. Delete a File Using File.delete()
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.io.File; public class DeleteFileExample { public static void main(String[] args) { File file = new File("example.txt"); if (file.delete()) { System.out.println("File deleted successfully: " + file.getName()); } else { System.out.println("Failed to delete the file or file does not exist."); } } } |
Returns true if deletion is successful, false...
π Java Read Files Java provides several ways to read data from files, mainly using: File + Scanner FileReader BufferedReader Files.readAllLines() (Java NIO) FileInputStream (for binary data) Β 1. Reading a File Using Scanner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.io.File; import java.util.Scanner; public class ReadFileScanner { public static void main(String[] args) { try { File file = new File("example.txt"); Scanner sc = new Scanner(file); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } sc.close(); } catch (Exception e) { System.out.println("Error: File not found!"); } } } |
...
π Java Write to Files Java provides several ways to write data to files using: FileWriter BufferedWriter PrintWriter Files.write() (modern method) FileOutputStream (for binary data) Β 1. Using FileWriter (Simple Text Writing)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.io.FileWriter; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, this is written using FileWriter!"); writer.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); } } } |
π§©...
π Java Create Files Java provides multiple ways to create a file using: java.io.File java.nio.file.Files (new and preferred method from Java 7+) β 1. Creating a File Using File Class The createNewFile() method creates...
π Java Files Java provides several classes to work with files, mainly from these packages: java.io.File java.io.FileWriter java.util.Scanner java.nio.file.Files (modern approach) You can perform tasks like: β Create a fileβ Write to a fileβ...
π§ Java Try-With-Resources (Automatic Resource Management) When working with resources such as: Files Database connections Network streams Buffered readers/writers You must close them after use β otherwise memory leaks or file locks may occur....
β οΈ Java Multiple Exceptions In Java, a single try block can throw more than one type of exception. To handle this, we can use: Multiple catch blocks Single catch block using multi-catch (Java 7+)...
β οΈ Java Exceptions β try…catch An exception is an event that disrupts the normal flow of a program. Exceptions usually occur during runtime β like dividing by zero, accessing invalid array indices, or opening...