Java Programming
Input and Output Fundamentals
1 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Overview
This lesson covers the following topics:
• Describe the basics of input and output in Java
• Read data from and write data to the console
• Use streams to read and write files
• Read and write objects by using serialization
2 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Basics of Input and Output
Java Applications read and write files. Whether you are
reading or writing data from files or transferring data
across the internet, input and output is the basis for
accomplishing that.
There are two options:
• java.io package
• java.nio.file package
3 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
java.io Package Limitations
The java.io package limitations are:
• Many methods fail to throw exceptions.
• Operations are missing (like, copy, move, and such).
• No support for symbolic links.
• Many methods failed to scale with large files.
4 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
java.nio.file Package
The java.nio.file package:
• Works more consistently across platforms.
• Provides improved access to more file attributes.
• Provides improved exception handling.
• Supports non-native files systems, plugged-in to the
system.
5 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
When Input and Output Occurs
Input occurs when Java:
• Reads the hierarchical file system to find directories.
• Reads physical files from the file system.
• Reads physical files through a symbolic link.
• Reads streams from other programs.
Output occurs when Java:
• Writes physical files to a directory.
• Writes physical files through a symbolic link.
• Writes streams to other programs.
6 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7
The old way has you construct:
• A new instance of File with a fully qualified file name,
which is one that includes the path and file name.
• A new FileReader with a File.
• A new BufferedReader with a FileReader.
7 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7 (cont.)
The changes between pre-Java 7 and Java 7 involve:
• The division of the Path and File into separate
interfaces.
• The delivery of the Paths and Files classes that
implement the new interfaces.
8 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7 (cont.)
•String
<Enterline =first-level introductory
"", fileContent = ""; paragraph,
Thissentence, or
creates a buffered
try { BufferedReader fileInput = Arial Regular) reader for file from the
phrase here.> (24 pt
new BufferedReader(new FileReader(new file system.
– <Enter second-level bullet text here.> (22 pt Arial Regular)
File("C:/BlueJ/Fellowship.txt")));
line = fileInput.readLine(); Reads one line at a
–<Enter= line
fileContent third-level
+ "\n"; bullet text here.> (20 pt Arial Regular)
time.
while (line != null) {
– <Enter fourth-level bullet text here.> (18 pt Arial Regular)
line = fileInput.readLine();
if (line–!=<Enter
null) fifth-level
fileContent += line
bullet } pt Arial Regular)
+ "/n"; (16
text here.>
fileInput.close(); }
catch(EOFException eofe) {
System.out.println("No more lines to read.");
System.exit(0); }
catch(IOException ioe) {
System.out.println("Error reading file.");
System.exit(0); }
System.out.println(fileContent);}
9 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7 (cont.)
• A static method call to Paths.get() returns a valid path.
• A static method call to Files.newBufferedReader()
returns a file as a BufferedReader instance.
• Inside the call to the newBufferedReader() method,
another static call is made to the Charset.forName()
method.
• This approach uses static method calls to return a
BufferedReader class instance.
10 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7 (cont.)
A static call returns an
String line = "", fileContent = "";Path p = Paths.get("C:/BlueJ/Fellowship.txt");
instance of an absolute file
•try
<Enter first-level introductory
{ BufferedReader fileInput = paragraph,path. sentence, or
Files.newBufferedReader(p
phrase here.> (24 pt Arial Regular)
,Charset.forName("ISO-8859-1")); Calls the static
line = fileInput.readLine();
– <Enter second-level bullet text here.> (22 pt Arial method to
Regular)
fileContent = line + "\n"; create new
Calls the static method
–<Enter
while (line != third-level
null) { bullet text here.> (20 pt Arial Regular)
BufferedReader
line = fileInput.readLine();
and returns a Charset
– <Enter fourth-level bullet text .
here.> (18 pt Arial Regular)
instance.
if (line != null) fileContent += line + "\n"; }
– <Enter}fifth-level bullet text here.> (16 pt Arial Regular)
fileInput.close();
catch(EOFException eofe) {
System.out.println("No more lines to read.");
System.exit(0); }
catch(IOException ioe) {
System.out.println("Error reading file.");
System.exit(0); }
System.out.println(fileContent);}
11 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading a File Prior to Java 7 (cont.)
The BufferedWriter.write() method writes the entire String
as a file. .
A static call returns an
instance of an absolute
file path.
public static void writeFile(String fileContent) {
Path p = Paths.get("C:/BlueJ/OutputFile.txt"); Calls the static
try { method and returns a
BufferedWriter bw = Charset instance.
Files.newBufferedWriter( p
, Charset.forName("ISO-8859-1")
Calls the static method to , StandardOpenOption.CREATE
create new BufferedWriter. , StandardOpenOption.APPEND );
bw.write(fileContent, 0, fileContent.length());
bw.close(); }
catch(IOException ioe) { The StandardOpenOption
System.out.println("Error reading file."); enum provides options for
System.exit(0); }} streams.
12 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
File Interface and Files Class
The new Path and File interfaces of Java 7 replace the old
way of managing directories and files.
The File interface and Files class:
• Creates files and symbolic links.
• Discovers and sets file permissions.
• Reads and writes files.
13 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Paths Interface and Paths Class
The Path interface and Paths class:
• Navigates the file system.
• Works with relative and absolute paths.
Paths are hierarchical structures:
• Sometimes called inverted trees because they start at
one top-most point and branch out downward.
• Path elements are directories or nodes.
• Directories may hold other directories or files.
14 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Absolute and Relative Paths
Absolute paths always start from:
• A logical drive letter on Windows.
• A / (forward slash) or mount point on Unix/Linux.
Relative paths are directories in a path and they
• May be the top-most (or root node) directory.
• May be the bottom-most (or leaf node) directory.
• May be any directory between the top and bottom.
15 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Absolute versus Relative File Paths
Absolute file path on Unix/Linux:
/home/username/data/filename
Relative file path on Unix/Linux:
data/filename
Absolute file path on Windows:
C:/Users/UserName/data/filename
Relative file path on Windows:
data/filename
16 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Symbolic Links
Symbolic Links are special files that reference another file
(sometimes called symlinks). Symbolic Links can
reference:
• A file in the same directory.
• A file in another directory of the same path.
• A file in another directory of a different path.
C:\> D:\>
data logs archive history
log_file log_file
(symlink) (file)
17 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Symbolic Links Example
For example, when a program has minor releases and the
compiled program should still call the program, how do
you manage that when the program name includes the
numbers that represent the minor release numbering?
Solution: Create a major release symbolic link that includes
only the major release number, and have it point to the
most current minor release. This way the compiled
program doesn't have to change as frequently.
18 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Java NIO.2 Interfaces and Classes
Locate a file or directory by using a system dependent
path with:
• java.nio.file.Path interface
• java.nio.file.Paths class
Using a Path class, perform operations on files and
directories:
• Java.nio.file.File inteface
• java.nio.file.Files class
19 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Java NIO.2 Interfaces and Classes (cont.)
Provides an interface to a file system and a factory for
creating a Path class instance and other file system object
instances.
• java.nio.file.FileSystem class
All methods throw IOException that access the file system
with Java NIO.2 classes. Interfaces give developers more
possibilities. The Paths and Files classes provide many
static methods that let you construct streams, which
simplifies the process.
20 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Construct a Path
This is a concrete class that
implements the abstract
•C
public static void main(String[] args) { FileSystem class.
FileSystem fs = FileSystems.getDefault();
Path p = fs.getPath("C://BlueJ//NIO2//DemoFile.txt");
System.out.println("Default Directory [" + p + "]"); }
A backslash must precede the backslash in a string path
when you use a Windows backslash. A \\ (double
backslash) is unnecessary in Windows. A / (forward slash)
is the preferred solution. NIO.2 converts the forward slash
to a backslash, and you could use the following string to
create a path:
Path p = fs.getPath("C:/BlueJ/NIO2/DemoFile.txt");
21 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Constructing Path Instances
These are all valid syntax for constructing Path instances,
producing the following Path values.
– <Enter second-level bullet text here.> (22 pt Arial Regular)
Path[] p = new Path[5];
p[0] =–<Enter
Paths.get("C://BlueJ//NIO2//DemoFile.txt");
third-level bullet text here.> (20 pt Arial Regular)
p[1] = Paths.get("C:/BlueJ/NIO2/DemoFile.txt");
– <Enter fourth-level bullet text here.> (18 pt Arial Regular)
p[2] = Paths.get("C:","BlueJ","NIO2","DemoFile.txt");
p[3] = Paths.get("DemoFile.txt");
– <Enter fifth-level bullet text here.> (16 pt Arial Regular)
P[4] = Paths.get(URL.create(“file:///~/DemoFile.txt”)
);}
Default File Path p[0] [C:/BlueJ/NIO2/DemoFile.txt]
Default File Path p[1] [C:/BlueJ/NIO2/DemoFile.txt]
Default File Path p[2] [C:/BlueJ/NIO2/DemoFile.txt]
Default File Path p[3] [DemoFile.txt]
Default File Path p[4] [/~/DemoFile.txt]
22 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Analyze a Path and its Contents
The instance methods return the values, except
isAbsolute(), which returns a boolean that has been
coverted to a String for display. The isAbsolute() method
returns true when the path contains the root node of a file
hierarchy, like / in Unix or Linux or C:\ in Windows.
p.getFileName() [DemoFile.txt] The getNameCount returns
p.getParent() [C:/BlueJ/NIO2] the number of nodes in the path.
p.getNameCount() [3]
p.isAbsolute() [true]
p.toAbsolutePath() [C:/BlueJ/NIO2/DemoFile.txt]
p.toUri() [file:///C:/BlueJ/NIO2/DemoFile.txt ]
23 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Remove Path Redundancies
The following symbols can be used to reduce
redundancies:
• The . (dot) refers to the present working directory in
Unix, Linux, or Windows operating systems.
• The .. (double dot) refers to the parent directory of the
present working directory.
• The normalize() method creates the direct path to the
absolute file path.
24 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Remove Path Redundancies (cont.)
This example navigates down to the IO directory, then up
using the .. notation, then down to the NIO2 (sibling)
directory.
Path rp = Paths.get("C:/BlueJ/IO/../NIO2//DemoFile.txt");
System.out.println("p.normalize() [" + rp.normalize() + "]");
It prints the normalized, or redundancy free path:
p.normalize() [C:/BlueJ/NIO2/DemoFile.txt];
25 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Working with Subpaths
A Path's subpath method is similar to a String's substring
method, but it segments a Path, not a String.
Given the beginning and ending index, this method
returns the part of the path from the beginning index to the
ending index.
Path subpath(int beginIndex, int endIndex);
26 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Working with Subpaths Example 1
In the following example:
• The beginning node is 0.
• The ending node is the maximum number of nodes in
the path, or the return value of getNameCount().
• Normalizes the first path by shortening it from 5 to 3
nodes.
Path sp = Paths.get("C:/BlueJ/IO/../NIO2/Path/DemoFile.txt");
System.out.println("p.subpath() [" + sp.getNameCount() +
"][" + sp.subpath(0,5) + "]");
System.out.println("p.subpath() [" + sp.getNameCount() +
"][" + (sp.normalize()).subpath(0,3) + "]");
27 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Working with Subpaths Example 2
The following normalizes the nodes and then uses the
getNameCount() method to find the last node in the path.
(sp.normalize()).subpath(0,sp.normalize().getNameCount()-1)
28 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Join Two Paths
The resolve() method allows:
• Adding a node as a String to a path.
• Removing a node as a String from a path.
Path.resolve(String path)
The following shows the syntax:
Path bp = Paths.get("C:/BlueJ");
Path np = Paths.get("NIO2/Path");
// Add a path not found in it. This adds the subpath to the path.
bp.resolve(np.toString())
// Remove a path found in it.
np.resolve(bp.toString())
This removes what was added
and Leaves: C:\BlueJ as the path.
29 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Find the Relative Path Between Two Paths
The relativize() method constructs a path from one
location to another when:
• It requires relative paths.
• It only works when working between nodes of the same
file directory tree (hierarchy).
• It raises an IllegalArgumentException when given a call
parameter in another directory tree.
// Declare Path instances. The relativize() method
Path p1 = Paths.get("NIO2"); only works with two
Path p2 = Paths.get("Projects"); relative paths.
// Output value of join between two paths.cd
System.out.println("p1.realativize(p1) [" +
p1.relativize(p2).toString() + "]");
30 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Paths Class is Link Aware
Every Paths method:
• Detects what to do when encountering a symbolic link.
• Provides configurations options for symbolic links.
The Files class provides these static methods for symbolic
links:
File.createSymbolicLine(Path, Path, FileAttribute <V>);
File.createLink(Path, Path);
File.readSymbolicLink(Path);
File.isSymbolicLink(Path);
31 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Paths Class is Link Aware (cont.)
• Target link must exist.
• No hard links allowed on directories.
• No hard links across partitions or volumes.
• Hard links behave like files and the isSymbolicLink()
method discovers them.
32 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Files Class Checks for File Existence
The Files class checks to see if files exist, or do not exist.
• By default, symbolic links are not followed.
• If the !exists() method and notExists() method are both
false, it means that they cannot determine whether the
file exists.
This is the outcome of checking the status of a CDROM in
the Windows operating system:
Files.exists(Path p, LinkOption options);
Files.notExists(Path p, LinkOption options);
33 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Files Class Checks File Properties
The Files class checks to see if files are:
• Readable
• Writeable
• Executable
• Hidden
• The same
34 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Files Class Checks File Properties (cont.)
The Files class provides these static methods for checking
file properties and duplication:
Files.isReadable(Path p);
Files.isWritable(Path p);
Files.isExecutable(Path p);
Files.isHidden(Path p);
Files.isSameFile(Path p1, Path p2);
35 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Creating Files and Directories
Create files with one of the following methods:
Files.createFile(Path p);
Files.createDirectory(Path p);
Create multiple levels of directories with this method:
Files.createDirectories(Path p);
36 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Deleting Files and Directories
Delete files, directories, or links with this method.
• Throws a NoSuchFileException,
DirectoryNotEmptyException, or IOException when the
file is not found or the directory holds files or directories.
Files.delete(Path p);
Delete files, directories, or links without exceptions by
using this method.
Files.deleteIfExists(Path p);
37 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Copying and Moving Files and Directories
Import the java.nio.file.StandardCopyOption.* package
when you want the ability to copy or move files and
directories. Copy or move files or directories with these
methods:
Files.copy(Path p, CopyOption ...);
Files.move(Path p, CopyOption ...);
An example would be:
Files.copy(source, target, REPLACE_EXISTING, NOFOLLOW_LINKS);
38 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
StandardCopyOption and LinkOption Enums
The StandardCopyOption and LinkOption enums are:
• REPLACE_EXISTING: Works with existing file or
directory.
• COPY_ATTRIBUTES: Copies related attributes.
• NOFOLLOW_LINKS: Disables following symbolic links.
39 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
StandardCopyOption and LinkOption Enums
Format
The options must be prefaced with StandardCopyOption
or LinkOption.
Examples:
• StandardCopyOption.REPLACE_EXISTING
• StandardCopyOption.COPY_ATTRIBUTES
• StandardCopyOption.NOFOLLOW_LINKS
• LinkOption.REPLACE_EXISTING
• LinkOption.COPY_ATTRIBUTES
• LinkOption.NOFOLLOW_LINKS
40 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
File Permissions
The relativize() method constructs a path from one
location to another when:
• It requires relative paths.
• It only works when working between nodes of the same
file directory tree (hierarchy).
• It raises an IllegalArgumentException when given a call
parameter in another directory tree.
41 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
File Permissions (cont.)
// Declare Path instances. The relativize() method only
Path p1 = Paths.get("NIO2"); works with two relative paths.
Path p2 = Paths.get("Projects");
// Output value of join between two paths.cd
System.out.println("p1.realativize(p1) [" +
p1.relativize(p2).toString() + "]");
42 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
File Permissions and Operating Systems
The file permissions differ from operating system to
operating system.
43 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Input and Output Stream Basics
Standard programming has three basic streams:
• Standard in (stdin), input to programs
• Standard out (stdout), output from programs
• Standard error (stderr), error messages from programs
Java has three basic streams:
• System.in an InputStream (like standard in)
• System.out a PrintStream (like standard out)
• System.err a PrintStream (like standard error)
44 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Input and Output Stream Diagram
Input device:
Keyboard, Standard Input
mouse, or touch
screen.
Program
Output device: Standard Out
Monitor or
printer.
Standard Error
45 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Java Stream Basics
Java provides specialized stream classes:
• Input Streams
• Output Streams
Java stream libraries:
• Simplify deployment
• Handle most types of input and output
46 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading an Input Stream by Character
Java reads an input stream:
• Character-by-character
• Line-by-line
private static String readEntry() {
try {
int c;
StringBuffer buffer = new StringBuffer();
c = System.in.read(); This reads the input
while (c != '\n' && c != -1) {
stream character-by-
buffer.append((char)c);
c = System.in.read(); } character.
return buffer.toString().trim(); }
catch (IOException e) {
return null; }}
47 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Reading an Input Stream by Line
Line-by-line reads require a BufferedReader, which is a
specialization of an IO Reader class. System.in provides a
static method to create an instance of an InputStream
class.
This is a static call to
construct an input stream
private static String readLine() { from the command-line.
String line = "";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
try { Create a BufferedReader
line = in.readLine(); } stream that provides the
catch (IOException e) {} readLine() method.
return line; }
This reads the input stream
line-by-line.
48 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Writing an Output Stream
Output to the console is typically managed by calling the
static System.out, which is a PrintStream. Other
alternatives require combining streams.
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
char[] input;
Uses a modified readEntry()
System.out.print("Enter a string: ");
input = readEntry();
method that returns an array
for (int i = 0; i < input.length; i++) { of char, which are then
if (input[i] != '\n' && input[i] != '\0') appended to a StringBuffer
sb.append(input[i]); } until the end of the output is
System.out.println(sb); } found.
System.out is PrintStream
that can be accessed by a
static call.
49 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Object Serialization
Object serialization is the process of encoding objects as
a byte stream, transmitting them, and reconstructing
objects from by decoding their byte stream.
• Encoding an object into a stream is serialization.
• Decoding a stream into an object is deserialization.
• Serialization is the standard method for Java beans.
• Serialized classes implement the Serializable interface.
50 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Use Serialization Wisely
Use serialization wisely because serialized classes:
• Are less flexible to change.
• May have more likelihood of bugs and security
vulnerabilities.
• Are more complex to test.
51 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Serializing and Deserializing
This serializes a file into an object.
public static void serialize( String outFile
, Object serializableObject)
throws IOException {
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject); }
This deserializes an object.
public static Object deSerialize(String serilizedObject)
throws FileNotFoundException
, IOException
, ClassNotFoundException {
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject(); }
52 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
The main() method tests serialization by:
• Serializing an object.
• Deserializing an object.
• Printing the transferred contents of the first object.
53 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing (cont.)
File objects can also be serialized.
public static void main(String[] args)
throws IOException
, FileNotFoundException
, ClassNotFoundException {
Serialization serial1 = new Serialization();
serialize("Fellowship.txt", serial1);
Serialization serial2 = (Serialization)
deSerialize("Fellowship.txt");
System.out.println(serial2.getSerializableIdentifier()); }
54 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Terminology
Key terms used in this lesson included:
• Absolute Path
• Back quoting
• Deserialization
• File Name
• Hard link
• Input Streams
• Inverted Tree
• Leaf Node
• Logical drive
55 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Terminology
Key terms used in this lesson included:
• Mount point
• Node Tree
• Normalize path
• Output Streams
• Path Name
• Relative Path
• Resolve path
• Root Node
56 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Terminology
Key terms used in this lesson included:
• Serialization
• Standard error
• Standard input
• Standard output
• Streams
• Symbolic link
• Tree
57 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Summary
In this lesson, you should have learned how to:
• Describe the basics of input and output in Java
• Read data from and write data to the console
• Use streams to read and write files
• Read and write objects by using serialization
58 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Input and Output Fundamentals
Practice
The exercises for this lesson cover the following topics:
• Input and output streams
• File reading and writing
• Serialization topics
• Implementing how you read standard in
• Implementing how you write standard out
• Implementing how you read and write files
• Implementing how you serialize objects
59 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.