3-5
Input and Output Fundamentals
JPS3L5 3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Files Class Checks for File Existence
JPS3L5 4
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Files Class Checks File Properties
JPS3L5 5
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Files Class Checks File Properties
• 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);
JPS3L5 6
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Creating Files and Directories
JPS3L5 7
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Creating Files and Directories Example
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
JPS3L5 8
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Deleting Files and Directories
JPS3L5 9
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Deleting Files and Directories Example
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
JPS3L5 10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Copying and Moving Files and Directories
JPS3L5 11
Copyright © 2015, 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.
JPS3L5 12
Copyright © 2015, 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
JPS3L5 13
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
File example
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
JPS3L5 14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
File example
… code continued from previous slide
Existing file is copied to the backup
try { directory
if(Files.exists(woF)){
if(Files.notExists(buD)){
Files.createDirectories(buD);
}//endif
Files.copy(woF, buF, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES);
} //endif
if(Files.notExists(woD))
Files.createDirectories(woD);
//endif
if(Files.notExists(woF))
Files.createFile(woF); If the required directory/file does not
//endif exist then they are created.
} //end try
catch (IOException x) {
System.err.println(x);
} //end catch
}// end of main
}//end of class FilesDemo
JPS3L5 15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
File Permissions
JPS3L5 16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
File Permissions
Path p1 = Paths.get("NIO2");
Path p2 = Paths.get("Projects");
JPS3L5 17
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
File Permissions and Operating Systems
JPS3L5 18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Input and Output Stream Basics
JPS3L5 19
Copyright © 2015, 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
JPS3L5 20
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Java Stream Basics
JPS3L5 21
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Reading an Input Stream by Character
catch (IOException e) {
return null;
}//endcatch
}//end method readEntry
JPS3L5 22
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Reading an Input Stream by Line
JPS3L5 23
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Reading an Input from file
JPS3L5 24
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Writing an Output Stream
JPS3L5 26
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Writing Output to File
JPS3L5 27
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Object Serialization
JPS3L5 28
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Use Serialization Wisely
JPS3L5 29
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Serializing and Deserializing
JPS3L5 30
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
JPS3L5 31
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
Example
• This will be used to create the object that you will serialize
and deserialize.
• For a class to be serialized successfully it must implement the
java.io.Serializable interface.
JPS3L5 32
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
JPS3L5 33
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
JPS3L5 34
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
… code continued from previous slide
public static Course deSerializeData(Course c){
try
{//try reading the file
FileInputStream fileIn = new FileInputStream("C:/BlueJ/details.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
c = (Course) in.readObject();
in.close();
fileIn.close();
return c;
}//end try
catch(IOException i)
{//catch any IO exception error that is thrown
i.printStackTrace();
return null;
}//end catch
catch(ClassNotFoundException e)
{//catch any error where the class is not found
System.out.println("Course class not found");
return null;
}//end catch
}//end method deSerializeData Code continues on next slide…
JPS3L5 35
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Testing Serializing and Deserializing
JPS3L5 36
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals
Terminology
JPS3L5 38
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Input and Output Fundamentals