Chapter 14 Exercises
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 in 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 from 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 new 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 integer numbers 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’ to 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 numbers.txt file and displays each value multiplied by 10 in the console.
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); // Convert the line to a number
int result = number * 10; // Multiply the number by 10
System.out.println(result); // Show 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 in 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 an employee’s data (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 in {
public static void main(String[] args) {
String file = "employee.txt"; // File name
// Write employee data to the file
try (FileWriter fw = new FileWriter(file)) {
fw.write("Name: John Doe\n");
fw.write("Age: 35\n");
fw.write("Position: Software Engineer\n");
} catch (IOException e) {
e.printStackTrace();
}
// Read data from file and display 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();
}
}
}