Chapter 14. Reading and Writing Text Files
Since we already have the record of the medicines stolen by Phil, it’s time to blackmail him. Peter will meet with him today. We have no doubt he will cooperate. If we presented the evidence, not only would he be fired, but he would also be involved in a legal process in which he could possibly be sentenced to some years in prison.
The plan is going perfectly. Nobody suspects anything, and we have already passed more than half the way. Before the last step, and finally getting out, we still need two things: to have a record of the security measures awaiting us in the last stretch before the exit and to access the guards’ communication.

I haven’t told you yet because it’s crazy, but the idea is to exit through the main door. It seems like suicide, but nobody will suspect that someone tried to escape through there. As you can imagine, it is the most guarded part of the prison and we must know exactly what we are facing.
To analyze the activity and tasks of the guards, we have some activity logs of the guards. These are in text format and we must create some Java programs to help us understand the information they contain and even modify it if necessary.
As usual, I have no idea what is happening outside. I hope Chani is taking care of everything and has communication with Rich. I hope that when I get out, I don’t find out that Rich has escaped and that all this effort has been in vain.
On the other hand, I am still in contact with my family, but I have lost some contact with my close friends. The truth is that at this moment I don’t want visitors. I am focused on the escape plan. I don’t want to think about what people might be saying about me. That could affect me. I am going to prove my innocence and show everyone who doubts me that they are wrong.

Importance of Reading and Writing Files in Java
Since you are taking your first steps in Java, it is normal that reading and writing files in Java is not a priority for you. Most programs do not need this functionality. However, it would not be smart to ignore that it is a fundamental tool for many applications. By reading a file, an application can process external data and work with it. Just as until now we have been giving instructions manually to our programs, now they will be able to follow those same guidelines from a text sheet.
Similarly, writing to files allows programs to save results, create logs, or store important data that can later be used or analyzed.
Another important advantage is that Java offers a wide variety of classes and methods within its library to handle files safely and easily. This facilitates the manipulation of large volumes of data, whether in text or binary format. Additionally, file handling in Java is essential for developing applications that require data persistence, such as information management systems or databases.
Reading Text Files
In my opinion, when you learn Java, the important thing is not to memorize concepts like a parrot, but to understand how programming works and comprehend the logic behind it. If you do it this way, it will be much easier for you; the feeling of progress will be much more satisfying and, besides, you will know how to program for the rest of your life. It is true that if you spend a long time without touching code, you might not remember much, but by dedicating a few hours, that knowledge quickly returns to your head.
After so many hours programming, you will already have quite an advanced knowledge of Java syntax. What at first seemed impossible to learn now makes sense and even seems easy. Not everyone gets this far, so congratulations.
For this particular topic, and much to my regret, we will have to memorize some things. For our programs to be able to read and write text, we have to import a series of classes.
Import the necessary classes to read files
import java.io.File; import java.io.FileReader;
import java.io.BufferedReader; import java.io.IOException;
Create a file object that represents the text file
Obviously, if you have a file you want to work with, you don’t need to create it, but what you do need is to know the path where the file is located on your computer.
File file = new File(“path/to/file.txt”);
This is an example of a real file path I use for example exercises: File file = new File(“C:\\Users\\ribeh\\IdeaProjects\\Project1\\activity.txt”);
Read the file using FileReader and BufferedReader
I would like to tell you that the program can already read the data from our file and even print it on the screen, but it is not so. There is still one more step.
We need to use try and catch, but since we already know how they work, there will be no problem.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (FileReader fr = new FileReader("activity");
BufferedReader br = new BufferedReader(fr)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line); // Prints each line of the file
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Writing Text Files
Again, we will have to import the necessary classes. In this case, they are these:
import java.io.File; import java.io.FileWriter;
import java.io.BufferedWriter; import java.io.IOException;
Of course, we will need to have the path of our file. I have already explained how the path should be:
File file = new File(“path/to/file.txt”);
This is an example of a real file path I use for example exercises: File file = new File(“C:\\Users\\ribeh\\IdeaProjects\\Project1\\activity.txt”);
Write to the file using FileWriter and BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String activity = "activity.txt"; // Define the file name
try (FileWriter fw = new FileWriter(activity);
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write("Hello, this is a new text file.");
bw.newLine(); // Adds a new line
bw.write("Here we can add more content.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Summary of what was learned
To read text files in Java, the first thing we must do is import the classes File, FileReader, BufferedReader, and IOException. To read a file, you must choose that document and copy its path. Then, you use FileReader and BufferedReader to read the file line by line and display its content. It is essential to handle possible exceptions with try and catch blocks so that the program can manage any error that arises. Fortunately, we learned how to do this in the previous topic.
Let’s look at the other side of the coin: writing to those text files. To start, you also need to import some classes, which in this case are: File, FileWriter, BufferedWriter, and IOException. Just like before, we will need the path of our file. Then, we will use FileWriter and BufferedWriter to write content to the file, adding new lines when necessary. It is crucial to handle exceptions in this process to ensure that any problem during writing is properly managed.
These steps allow you to read and write text files in Java effectively, managing errors and exceptions in a controlled way.