AI May 15, 2023 5 min read

Module 37: File Handling in Java

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

📂 Introduction

Java provides powerful classes for reading and writing files in the java.io and java.nio packages. File handling is essential for data persistence and working with external data sources.

📄 Reading from a File

                    import java.io.File;
                    import java.io.FileNotFoundException;
                    import java.util.Scanner;
                    
                    public class ReadFileExample {
                      public static void main(String[] args) {
                        try {
                          File file = new File("example.txt");
                          Scanner reader = new Scanner(file);
                          while (reader.hasNextLine()) {
                            String data = reader.nextLine();
                            System.out.println(data);
                          }
                          reader.close();
                        } catch (FileNotFoundException e) {
                          System.out.println("An error occurred.");
                          e.printStackTrace();
                        }
                      }
                    }

📝 Writing to a File

                    import java.io.FileWriter;
                    import java.io.IOException;
                    
                    public class WriteFileExample {
                      public static void main(String[] args) {
                        try {
                          FileWriter writer = new FileWriter("output.txt");
                          writer.write("This is written to the file.");
                          writer.close();
                          System.out.println("Successfully wrote to the file.");
                        } catch (IOException e) {
                          System.out.println("An error occurred.");
                          e.printStackTrace();
                        }
                      }
                    }

📌 Tips

  • Always close file readers/writers to avoid memory leaks.
  • Use try-with-resources for automatic resource management.
  • Check if the file exists before reading it.
Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More →
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More →

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.