---
title: "How to read and write files in Java? Please provide a sample."  
description: "How to read and write files in Java? Please provide a sample."  
author: "Revati S Misra"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159837/how-to-read-and-write-files-in-java-please-provide-a-sample  
category: "java"  
tags: ["java", "file"]  
reading_time: 2 minutes  

---

# How to read and write files in Java? Please provide a sample.

How to read and write [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)? Please provide a sample.

## Replies

### Reply by Aryan Kumar

There are two ways to read and write files in Java:

- **Using the FileInputStream and FileOutputStream classes:** These classes provide a low-level interface for reading and writing files.
- **Using the Scanner and PrintWriter classes:** These classes provide a higher-level interface for reading and writing files.

Here is a sample code that shows how to read and write a file using the FileInputStream and FileOutputStream classes:

Java

```plaintext
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileIOExample
{
    public static void main(String[] args) throws Exception
    {
        // Create a FileInputStream object for the file that we want to read.
        FileInputStream inputStream = new FileInputStream("myfile.txt");

        // Create a FileOutputStream object for the file that we want to write to.
        FileOutputStream outputStream = new FileOutputStream("myfile_copy.txt");

        // Read the contents of the file and write them to the new file.
        int data;
        while ((data = inputStream.read()) != -1)
        {
            outputStream.write(data);
        }

        // Close the streams.
        inputStream.close();
        outputStream.close();
    }
}
```

Here is a sample code that shows how to read and write files using the Scanner and PrintWriter classes:

Java

```plaintext
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileIOExample
{
    public static void main(String[] args) throws FileNotFoundException
    {
        // Create a Scanner object for the file that we want to read.
        Scanner scanner = new Scanner(new File("myfile.txt"));

        // Create a PrintWriter object for the file that we want to write to.
        PrintWriter writer = new PrintWriter("myfile_copy.txt");

        // Read the contents of the file and write them to the new file.
        while (scanner.hasNextLine())
        {
            writer.println(scanner.nextLine());
        }

        // Close the streams.
        scanner.close();
        writer.close();
    }
}
```


---

Original Source: https://www.mindstick.com/forum/159837/how-to-read-and-write-files-in-java-please-provide-a-sample

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
