Chapter 14: I/O
Cheatsheet

Chapter 14 Cheatsheet

❗Important

  • PrintStream classes do not throw any checked exceptions
  • Path is an interface
  • Paths is a class (Paths has an s in it, so is class)

Method/Constructor to create a FileSystem

Using FileSystems

public static FileSystem getDefault()
➡️ FileSystem mySystem = FileSystems.getDefault();

Method/Constructor to create a File

Using File

public File(String pathname)
➡️ File file1 = new File("/Users/username/IdeaProjects/ocpjava17/test.txt");
 
public File(File parent, string child)
➡️ File file2 = File(new File("/root/ocpjava17/test"), "test.java");
 
public File(String parent, String child)
➡️ File file3 = File("/root/ocpjava17", "test.java");

Using Path

public default File toFile()
➡️ File file3 = File("/root/ocpjava17", "test.java");

Method/Constructor to create a Path

Using FileSystem

public Path getPath(String first, String... more)

Using File

public Path toPath()

Using Path

public static Path of(String first, String... more)
public static Path get(URI uri)
 
public static Path get(String first, String... more)
public static Path get(URI uri)

NIO.2 Path methods

public String toString()
public int getNameCount()
 
public Path getRoot()
public Path getParent()
public Path getFileName()
public Path getName(int index)
 
public Path resolve(String p)
public Path resolve(Path p)
public Path relativize(Path p)
 
public Path normalize()
public Path toRealPath()
public Path subpath(int beginIndex, int endIndex)

NIO.2 optional parameters

LinkOption

  • LinkOption.NOFOLLOW_LINKS

StandardCopyOption

  • StandardCopyOption.ATOMIC_MOVE
  • StandardCopyOption.COPY_ATTRIBUTES
  • StandardCopyOption.REPLACE_EXISTING

StandardOpenOption

  • StandardOpenOption.APPEND
  • StandardOpenOption.CREATE
  • StandardOpenOption.CREATE_NEW
  • StandardOpenOption.READ
  • StandardOpenOption.TRUNCATE_EXISTING
  • StandardOpenOption.WRITE

FileVisitOption

  • FileVisitOption.FOLLOW_LINKS

The java.io abstract stream base classes

  • InputStream vs OutputStream
  • Reader vs Writer

The java.io concrete I/O stream classes

Low level

  • FileInputStream vs FileOutputStream
  • FileReader vs FileWriter

High level

  • BufferedInputStream vs BufferedOutputStream
  • BufferedReader vs BufferedWriter
  • ObjectInputStream vs ObjectOutputStream
  • PrintStream vs PrintWriter