Java I/O API



Java SE 11 Developer certification (exam number 1Z0-819) here

Java I/O API

Headlines
Console I/O streams

Rule(s)

Example

static String readEntry(String prompt) {
    try {
        StringBuilder buffer = new StringBuilder(); // 'StringBuilder' is faster than 'StringBuffer buffer = new StringBuffer();'...
        System.out.println(prompt);
        System.out.flush();
        int c = System.in.read();
        while (c != '\n' && c != -1) {
            buffer.append((char) c);
            c = System.in.read();
        }
        return buffer.toString().trim();
    } catch (java.io.IOException ioe) {
        System.err.println("Fatal I/O error... " + ioe.getMessage());
        return "";
    }
}

Example reading data from Operating System (OS) process

Example File system I/O

Serialization is the binary (and then non-structured) storage of objects within files

Rule(s)

Example

public class Individual implements java.io.Serializable {
    // '_age' is made 'transient' because its value may be obsolete at the time when 'Individual' objects are reloaded:
    private transient int _age; 
    …

Example

public class GPAO implements java.io.Serializable { // Non-serializable instance fields must be marked 'transient'
    static final long Serial_version_id = 1L; // Versioning
    …

Rule(s)

Example

import java.io.*;
…
String filename = "gpao.ser";
GPAO gpao = new GPAO(…);
FileOutputStream fos = null;
ObjectOutputStream ous = null;
try {
    fos = new FileOutputStream(filename);
    ous = new ObjectOutputStream(fos);
    ous.writeObject(gpao);
    ous.close();
}
catch(IOException ioe) {
    …
}

Rule(s)

Example

import java.io.*;
…
java.beans.XMLEncoder e = new java.beans.XMLEncoder(new BufferedOutputStream(new FileOutputStream ("gpao.xml")));
e.writeObject(gpao);
e.close();
…
java.beans.XMLDecoder d = new java.beans.XMLDecoder(new BufferedInputStream(new FileInputStream ("gpao.xml")));
GPAO gpao = (GPAO)d.readObject();
d.close();
java.nio.file API

Scenario(s)

From Java 7, java.nio.file.FileSystems, java.nio.file.Files or java.nio.file.Paths (list is not exhaustive, see also Java 11 java.nio.file API) are utility classes for quicker and non-blocking file manipulation. In this scope, one may imagine the creation a My_Web subfolder (with deletion if already existing) from an existing Web folder. Next a template index.html file in Web is copied to My_Web.

Example (creation)

java.nio.file.Files.createDirectories(java.nio.file.FileSystems.getDefault().getPath("Web").resolve("My_Web"));
index_html_file = java.nio.file.FileSystems.getDefault().getPath("Web" + java.io.File.separator + "My_Web" + java.io.File.separator + "index.html");
java.nio.file.Files.copy((new java.io.File("Web" + java.io.File.separator + "index.html")).toPath(), index_html_file, java.nio.file.StandardCopyOption.REPLACE_EXISTING);

Example (deletion)

java.nio.file.Files.deleteIfExists(index_html_file);
java.nio.file.Files.deleteIfExists(java.nio.file.FileSystems.getDefault().getPath("Web").resolve("My_Web"));

Example (character-based reading and writing)

java.util.List<String> lines = java.nio.file.Files.readAllLines(index_html_file, java.nio.charset.Charset.defaultCharset());
for (String line : lines) {
    if (line.contains("FB")) lines.set(lines.indexOf(line), "Franck Barbier");
}
java.nio.file.Files.write(index_html_file, lines, java.nio.charset.Charset.defaultCharset());