Programs use variables to store information:

  • Input data.
  • Calculated results.
  • Intermediate values generated throughout the calculation.

All this information is ephemeral: when the program finishes, everything disappears. But for many applications, it will be important to be able to store data permanently.

A file could be defined as:

"A set of bits stored on a device, and accessible through an access path (pathname) that identifies it."

This low-level version, although completely true, is too simple...

Therefore, we will define several criteria to distinguish different subcategories of files. These file types will differ from a programming point of view: each of them will provide different functionalities (methods) for their manipulation.

The content criterion:

We know that manipulating numbers is different from manipulating Strings, even though under the hood both end up being bits in the computer's memory. That is why, when we manipulate files, we will distinguish two classes of files depending on the type of data they contain:

  • Character (or text) files
  • Byte (or binary) files

A text file is one formed exclusively by characters and can be created and viewed using an editor. Read and write operations will work with characters. For example, files with Java code are text files. On the other hand, a binary file is no longer formed by characters; instead, the bytes it contains can represent other things such as numbers, images, sound...

The access mode criterion.

There are two basic modes of access to the information contained in a file:

  • Sequential.
  • Direct access.

In sequential mode, the file's information is a sequence of bytes (or characters) such that to access the i-th byte (or character)—it is named that way when speaking in a general case where we do not know the exact number—the previous i-1 bytes must have been accessed beforehand.

Direct access mode allows us to directly access the information of the i-th byte. A well-known example of direct access can be found with vectors (arrays).

First, we can look at a code example that writes in stream mode:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException;
/** * Clase que lee y escribe el fichero en modo Stream */
public class Stream {
   public static void main(String[] args) throws IOException {
        FileInputStream in = null; 
        FileOutputStream out = null;
        try{
            in = new FileInputStream("entrada.txt"); 
            out = new FileOutputStream("salida.txt"); 
            int c;
            while ((c = in.read()) != -1) {
                System.out.println(c);                 
                out.write(c);             
            }
        }finally {
            if (in != null) {
                in.close(); 
            }
            if (out != null) {
                out.close();             
            }
        }

    }
}

Download.

<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/DZH6cctVGFI" width="560"></iframe>

Another option is to load it into memory:

import java.io.BufferedReader; 
import java.io.FileReader;
 import java.io.FileWriter; 
import java.io.PrintWriter;
 import java.io.IOException;
/** * Clase que lee y escribe el fichero en memoria */ 
public class ReaderWritter {
    public static void main(String[] args) throws IOException {
        BufferedReader inputStream = null;
        PrintWriter outputStream = null;
        try {
            inputStream = new BufferedReader(new FileReader("entrada.txt")); 
            outputStream = new PrintWriter(new FileWriter("salida.txt"));
            String l;
            while ((l = inputStream.readLine()) != null) {
                System.out.println(l); 
                outputStream.println(l); 
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
                }
            if (outputStream != null) {
                outputStream.close();
                }
        }
    }
}

Download.

<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/NFYSq2vL3uU" width="560"></iframe>

Download all.