OCP - 4

NIO.2, JDBC

NIO.2, JDBC


Fichier Détails

Cartes-fiches 66
Langue English
Catégorie Informatique
Niveau Autres
Crée / Actualisé 30.04.2019 / 08.01.2020
Lien de web
https://card2brain.ch/box/20190430_ocp_4
Intégrer
<iframe src="https://card2brain.ch/box/20190430_ocp_4/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

What is the goal of the NIO.2 API?

To provide a replacement for the java.io.FIle class

What is the java.nio.file.Path class for?

It represents a path on the storage system to a file or directory. Its the direct replacement for the java.io.File class.

It can represent a File, Directory or Symlink

How can you construct a Path object?

  1. Using the Paths factory class, eg. Phats.get(...)
    1. by prividing one or multiple Strings
    2. by providing a URI
  2. Using a FileSystem instance, eg. Filesystem.getPath(...)
  3. From a Legacy File instance: aFIle.toPath()

Note that Path is an Interface!

Does it matter which path separator you use when constructing a Path?

Usually not, as most JVMs support both forward and backward slashes.

How can you simply construct a phat without using a path separator

Using the Path.get(String, String...) varargs overload

What happens when constructing a path with a URI to a relative path?

Paths.get(new URI("file://relative/path"));

An IllegalArgumentException is thrown

What has to be considered when using the URI constructor?

It throws a checked URISyntaxException

How can you convert a Path to a URI

Using the toUri() method

What is the difference between FileSystems.getDefault().getPath(...) and Paths.get()?

Its identical, the Paths API is just convenience

When shoud the FileSystem class be used over the Paths class to construct paths?

When working with remote file systems, eg. FTP

Is it necessary for a file to exist when working with a Path object?

For most operations: no. There are a hand ful of methods - such as Path.toRealPath() that require the file to exist and throw an Exception if the file is not available

What are the mothods Path.getNameCount and Path.getName for?

They can be used to access Fragments of a path

Paths.of("/foo/baa/x").getNameCount() // 3

Paths.of("/foo/baa/x").getName(1) // Paths.of("baa")

What does Path.getParent(".") return?

null

What does Path.getParent("/") return?

null

What does Path.getParent("/foo") return?

Paths.get("/")

What does Paths.get(".").getRoot() return?

null

What happens, if the underlying path does not exist?

Paths.get("x/y/z").toAbsolutePath()

It works as expected, the underlying files must not exist!

What is Path.subpath() for?

It is the equivalent of substring for paths: eg. Paths.get("x/y/z").subpath(1,3) ==> Path.of("y/z")

 

What happens when the indices are out of bounds when calling Path.subpath?

An IllegalArgumentException is thrown

What is the result of this:

Paths.get("/home/foo").relativize(Paths.get("/home/foo/bar"))

Path.get("bar")

What is the result of this:

Paths.get("relative").relativize(Paths.get("/home/foo/bar"))

An java.lang.IllegalArgumentException is thrown as both paths must be either absolute or relative

What is thre result of this:

Paths.get("relative").relativize(Paths.get("relative/foo/bar"))

Path.get("foo/bar")

How can two path objects be joined?

Using the Path.resolve(path) Method

What is the result of this?

Paths.get("relative").resolve(Paths.get("relative/foo/bar"))

Path.get("relative/relative/foo/bar")

What is the result of this?

Paths.get("relative").resolve(Paths.get("/home/foo/bar"))

Path.get("/home/foo/bar")

What happens when calling normalize(Path) on a path that does not point to an existing element on the fs?

It works anyways!

What does the Path.toRealPath() method do?

  • The equvalent to the follwing calls:
    • p2 = path.toAbsolutePath().normalize()
    • if(FIles.exist(p2) == false) throw new java.nio.file.NoSuchFileException("...")

How can you get the current working directory?

By calling Paths.get(".").toRealPath()

In what aspect does the Files API differ in comparison to Path and Paths?

Most of the Options with the Files class will throw an exception if the file to which the Pqth referes does not exist

What does the Files.isSameFile(path, path)  do?

It checks weather the two paths point to the same file, which includes the lookup of symlinks.

It does not compare the contents of two files!

How can new Directories be created in java.nio?

Files.createDirectory(Paths.get("..."))

Files.createDirectories(Paths.get("..."))

How can you copy/move/rename files with java.nio2?

Using the Files.copy and Files.move methods

Does Files.copy() of a directory perfor a deep copy or shallow copy?

A shallow copy!

What happens when calling Files.move(srcDir, targetDir), when targetDir is on a different underlying drive?

It will fail if the given directory is not empty

How can you get a Reader/Writer InputStream/OutputStream of a File?

Using the factory methods on the Files clas:

  • Files.newBufferedReader(path)
  • Files.newBufferedWriter(path)
  • Files.newInputStream(path)
  • Files.newOutputStream(path)

What does Files.readAllLines() do?

It will read all Lines into a List = read everything in Memory (DANGEROUS -> Out of Memory)!

What shorthands for Metadata retreival exist on the Files class?

  • Files.isDirectory(path)
  • Files.isRegularFile(path)
  • Files.isSymbolicLink(path)
  • Files.isHidden(path)
  • Files.isReadable(path)
  • Files.isExecutable(path)
  • Files.size(path)
  • Files.getLastModifiedTime(path) / Files.setLastModifiedTime(path)

What is the result of Files.size(directory)

It is undefined behaviour!

What type is returned when calling Files.getOwner(path)

A UserPrincipal instance

How can you change the owner of a path?

userPrincipal = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName("john")

Files.setOwner(path, userPrincipal)