Get up to 80 % extra points for free! More info:

Lesson 2 - Introduction to working with files in Java

In the previous lesson, Exceptions In Java, we went over exceptions in Java, which are necessary for working with files since they allow us to react to errors properly. In today's tutorial, we're going to talk about files in general and the writing privileges in Windows. After reading this lesson, you will be ready to work with files.

Data, i.e. objects stored in memory, is lost once the application is terminated. If we want to ensure that the application data is persistent, we'd have to save it when the application is closing and load it back when we run it again. There are a lot of ways to store application data, each has some advantages and disadvantages. Generally, we store application data in one of the following ways:

  • Text files with a flat structure (e.g. .txt and .csv files)
  • Text files with an internal hierarchy (e.g. .json or .xml files)
  • Binary files (simple memory dump into a file)
  • Databases

Each data-storing method has its advantages and disadvantages. During the course, we'll explain and try them out. We'll focus mainly on how to store objects into files and load them back after the application has been terminated. Afterward, we'll move on to the database course.

Writing privileges in Windows

Windows 7 writing privileges UAC - Files and I/O in Java

Since Windows Vista, Windows operating systems include what is known as UAC (User Account Control). This technology prohibits writing to the system disk partition (usually C:\) in case the writing operation is not allowed by the administrator or it's somewhere else than to the user's personal folders. This restriction obviously filled internet forums and broke many applications. However, the main thing here is that this restriction is correct and only prevents poorly written applications from changing everything they want to. Linux operating systems have had similar restrictions in order for a long time.

The Windows system concept includes user accounts. Each user account is represented by a folder (usually C:\Users\username). This folder contains, among others, documents, the desktop and the applications data folder. This folder can be accessed by entering the %appdata% key to a path. Type in %appdata% into the Windows explorer's path field and hit Enter. You will see that many of your installed applications have subfolders in this folder. Here's where you're meant to save your applications' settings and other data. Your application will need to create its own folder here named after itself and use it to save data. Using this folder, we won't be restricted by the UAC and our application will also be able to store different settings for different users, without explicitly programming it in Java. %appdata% always points to the AppData of the user who is currently logged in.

A big mistake that people would make was that they would make their programs store their data to the folder where the program was installed. The program worked if it was saved on their desktop or on old versions of Windows. However, if it was installed into the Program Files/ folder on the C drive, it would crash with an error when writing to the disk. Windows partially emulated this sort of thing for some applications, but we won't get into that. As you may have guessed, the problem can be worked around by running the application as administrators, however, this is an improper solution.

In this tutorial, we'll create a folder for our application in the AppData folder. In other tutorials, this part won't be included so the code for various file formats is as simple as possible. If you run the application from your computer, everything should work properly. Once you publish it, however, you'll have to modify it to use AppData. Now we know where we're going to write, and we have introduced exceptions. Let's get to actually working with files!

Streams

In Java, there are streams which are known as generic "data streams". We can connect to a stream and then load data from it or write data to it. Streams are theoretically universal and the same stream may lead to the RAM or to a file on the disk. Working with files is realized through streams. All of the classes that we'll mention in the future wrap a stream somehow or work with one. However, we don't need to work with them explicitly.

In this course, we'll deal with one file type in each lesson and introduce a class that is designed for it. Now, let's look at the very basics of how to create a folder for our application in the current user's AppData folder. We'll use the File class for working with files and folders. Let's write an application that will have its own folder in AppData and checks whether there is a database.dat file within said folder.

String path = "";

path = System.getenv("APPDATA") + File.separator + "IctSocial";
File fPath = new File(path);
if (!fPath.isDirectory()) {
    if (!fPath.mkdirs()) {
        System.out.printf("Unable to create the folder %s, check your privileges.", path);
    }
}

File fDatabase = new File(path + File.separator + "database.dat");
if (fDatabase.exists()) {
    try {
        // Place your code for loading your settings from the file here
    } catch (Exception e) {
        System.out.printf("Error while loading the settings: %s", e.getMessage());
    }
} else {
    try {
        // Place your code for creating your settings here
    } catch (Exception e) {
        System.out.printf("Error while creating the settings: %s", e.getMessage());
    }
}

First, we store the path to our application's folder to a path string. We get the AppData location using the following bit of code:

System.getenv("APPDATA")

For Windows 7 and 8 the method returns something like this:

C:\Users\your_name\AppData\Roaming

We'll add a folder with our application's name to the path as well. The name I chose is IctSocial/, you may use your own. It should now look like this:

C:\Users\your_name\AppData\Roaming\IctSocial

If the given folder doesn't exist (it's the first run of the application), we'll make one. Then, we'll check whether the file whose data we want to load exists. If so, we load the data. If it doesn't, we'll create the file and save a default bit of data to it. We could also skip creating the file overall, it all depends on your application's needs.

Once we run our application, a new folder will appear in %appdata%:

The Appdata folder and creating an application folder here in C# .NET - Files and I/O in Java

This was a quick introduction on how to store an application's data properly. At this point, you should be very familiar with what exceptions are as well. Now, you know all that you need to work with your first file format. In the next lesson, Working with text files in Java, we'll conduct a few file operations on text files. To make things clear, I won't deal with writing privileges in future lessons so as to make keep the code simple. In other words, our programs will only work in user folders (when executed from Visual Studio). Feel free to come back here and adjust your code to fit the distributable format once you finish your application.


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 6x (19.68 kB)
Application includes source codes in language Java

 

Previous article
Exceptions In Java
All articles in this section
Files and I/O in Java
Skip article
(not recommended)
Working with text files in Java
Article has been written for you by David Capka Hartinger
Avatar
User rating:
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities