Learn Java Easy. Chapter 14 Exercises

Exercises Topic 14 – Reading and Writing Text Files in Java

Exercises Topic 14

Exercise 1. Write a program that saves the phrase “Hello World” in a file called output.txt.

Solution:

import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (FileWriter writer = new FileWriter("output.txt")) {
            writer.write("Hello World");
            System.out.println("Text written to output.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 2. Write a program that reads the content of the file called output.txt and displays it on the console.

Solution:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (BufferedReader reader = new BufferedReader(new FileReader("output.txt"))) {
            String line;

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 3. Write a program that copies the content of output.txt to destination.txt

Solution:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (FileReader reader = new FileReader("output.txt");
             FileWriter writer = new FileWriter("destination.txt")) {

            int c;

            while ((c = reader.read()) != -1) {
                writer.write(c);
            }

            System.out.println("Content copied from output.txt to destination.txt");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 4. Write a program that counts the number of words in the file called destination.txt

Solution:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        int wordCount = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader("destination.txt"))) {

            String line;

            while ((line = reader.readLine()) != null) {
                String[] words = line.split("\\s+");
                wordCount += words.length;
            }

            System.out.println("Number of words in destination.txt: " + wordCount);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 5. Write a program that saves the numbers from 1 to 10 in a file called numbers.txt, with each number on a line.

Solution:

import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (FileWriter writer = new FileWriter("numbers.txt")) {
            for (int i = 1; i <= 10; i++) {
                writer.write(i + "\n");
            }

            System.out.println("Numbers written to numbers.txt");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 6. Write a program that reads integers from a file called numbers.txt and calculates the sum of these numbers.

Solution:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        int sum = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader("numbers.txt"))) {

            String line;

            while ((line = reader.readLine()) != null) {
                sum += Integer.parseInt(line);
            }

            System.out.println("Sum of numbers in numbers.txt: " + sum);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Exercise 7. Write a program that adds the phrase “End of file” at the end of a file called log.txt.

Solution:

import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (FileWriter writer = new FileWriter("log.txt", true)) {
            writer.write("End of file\n");
            System.out.println("Text added to log.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 8. Write a program that reads the content of the file numbers.txt and displays it on the console with each value multiplied by 10.

Solution:

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        String file = "numbers.txt"; // File containing the numbers

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {

            String line;

            while ((line = br.readLine()) != null) {
                int number = Integer.parseInt(line); // Converts the line to a number
                int result = number * 10; // Multiplies the number by 10
                System.out.println(result); // Displays the result
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            System.out.println("The file contains non-numeric data.");
        }

    }
}

Exercise 9. Write a program that writes bytes from 0 to 255 to a file called output.bin.

Solution:

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        try (FileOutputStream fos = new FileOutputStream("output.bin")) {
            for (int i = 0; i <= 255; i++) {
                fos.write(i);
            }

            System.out.println("Bytes written to output.bin");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Exercise 10. Write a program that writes the data of a worker (Name, Age, and Position) to a file. Then, the program will read this data and display it on the screen.

Solution:

import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        String file = "worker.txt"; // File name

        // Write worker data to the file
        try (FileWriter fw = new FileWriter(file)) {
            fw.write("Name: John Perez\n");
            fw.write("Age: 35\n");
            fw.write("Position: Software Engineer\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Read data from the file and display it on screen
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line); // Display each line on screen
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏