File I/O in Java

 File I/O in Java


Java provides built-in classes for reading and writing files. You can use these classes to read data from files, write data to files, and create new files.


Example code:


csharp


try {

    FileWriter writer = new FileWriter("output.txt");

    writer.write("Hello, world!");

    writer.close();

} catch (IOException e) {

    System.out.println("An error occurred: " + e.getMessage());

}


try {

    BufferedReader reader = new BufferedReader(new FileReader("input.txt"));

    String line = reader.readLine();

    while (line != null) {

        System.out.println(line);

        line = reader.readLine();

    }

    reader.close();

} catch (IOException e) {

    System.out.println("An error occurred: " + e.getMessage());

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...