Java I/O API



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

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

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 PLM 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 = "plm.ser";
PLM plm = new PLM(…);
FileOutputStream fos = null;
ObjectOutputStream ous = null;
try {
    fos = new FileOutputStream(filename);
    ous = new ObjectOutputStream(fos);
    ous.writeObject(plm);
    ous.close();
}
catch(IOException ioe) {
    …
}

Rule(s)

Example

import java.io.*;
…
java.beans.XMLEncoder e = new java.beans.XMLEncoder(new BufferedOutputStream(new FileOutputStream ("plm.xml")));
e.writeObject(plm);
e.close();
…
java.beans.XMLDecoder d = new java.beans.XMLDecoder(new BufferedInputStream(new FileInputStream ("plm.xml")));
PLM plm = (PLM)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());