Java I/O API
- Read and write console and file data using I/O Streams
- Implement serialization and deserialization techniques on Java objects
- Handle file system objects using
java.nio.file
API
Rule(s)
System
class owns thein
,out
, anderr
class attributes as basic console I/O streamsExample
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)
- Serialization is the binary (and then non-structured) storage of objects within files
- Serializable objects must implement the
java.io.Serializable
interfaceExample
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)
- Serialization (i.e., binary storage) operates by means of files.
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)
- XML-based serialization (and deserialization) allows the preservation of some structuting in files for external processing. XML-based serialization relies on the
java.beans
core library.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
APIScenario(s)
From Java 7,
java.nio.file.FileSystems
,java.nio.file.Files
orjava.nio.file.Paths
(list is not exhaustive, see also Java 11java.nio.file
API ☛) are utility classes for quicker and non-blocking file manipulation. In this scope, one may imagine the creation aMy_Web
subfolder (with deletion if already existing) from an existingWeb
folder. Next a templateindex.html
file inWeb
is copied toMy_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());