NIO Flashcards

1
Q

How can we create directories?

A

Files.createDirectory(Path.of(“..”))
Files.createDirectories(Path.of(“..”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we get File from Path

A

path.toFile()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we get Path from File

A

file.toPath()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What should we watch out when using File?

A

If file we work with is regular file or directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

file.exists()

A

Checks whether the file or directory denoted by this abstract pathname exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we check if a file or directory exists?

A

file.exists()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

file.getAbsolutePath()

A

Returns the absolute pathname string of this abstract pathname.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we get the absolute path of a file?

A

file.getAbsolutePath()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

file.isDirectory()

A

Tests whether the file denoted by this abstract pathname is a directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we check if a path represents a directory?

A

file.isDirectory()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

file.getParent()

A

Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we get the parent directory of a file?

A

file.getParent()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

file.length()

A

Returns the length of the file denoted by this abstract pathname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we get the size of a file?

A

file.length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Files.exists(path)

A

Tests whether a file or directory exists at the specified path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we check if a file or directory exists using the Files class?

A

Files.exists(path)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Files.isRegularFile(path)

A

Tests whether a file is a regular file with opaque content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can we check if a path represents a regular file?

A

Files.isRegularFile(path)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Files.size(path)

A

Returns the size of a file (in bytes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can we get the size of a file using the Files class?

A

Files.size(path)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Files.list(path)

A

Returns a lazily populated Stream of entries in the directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can we get a stream of entries in a directory?

A

Files.list(path)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How can we create Paths?

A

Path.of()
Path.get()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

path.getNameCount()

A

Returns the number of name elements in the path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How can we get the number of name elements in a path?

A

path.getNameCount()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

path.getName(i)

A

Returns a name element of this path as a Path object. The index parameter specifies which name element to return, counting from zero (the root component, if present).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How can we get a specific name element from a path?

A

path.getName(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Are paths immutable?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How can we prove paths immutability?

A

path.resolve(“dir”). this sets new path that we need to use with variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What happens if we mix absolute and relative paths?

A

Absolute paths trumps relative paths

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

path.getFileName()

A

Returns the name of the file or directory denoted by this path as a Path object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How can we get the file name from a path?

A

path.getFileName()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

path.getRoot()

A

Returns the root component of this path as a Path object, or null if this path does not have a root component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How can we get the root component of a path?

A

path.getRoot()

34
Q

currentParent.getParent()

A

Returns the parent path, or null if this path does not have a parent.

35
Q

How can we get the parent directory of a path?

A

currentParent.getParent()

36
Q

path.toRealPath()

A

Returns the real path of an existing file, eliminating any symbolic links in its path.

37
Q

How can we resolve symbolic links and get the real path of a file?

A

path.toRealPath()

38
Q

How can we get parts of path?

A

path.subpath(start, end)
We need to add 1 to the end.
path.subpath(1,1) causes IllegalArgumentException

39
Q

How can we get path to get from one path to another path?

A

path1.relativize(path2)

Ask yourself what path needs to be created to go from one path to another.

Path path1 = Paths.get(“/home/user/documents”);
Path path2 = Paths.get(“/home/user/pictures”);

Path relativePath = path1.relativize(path2);
System.out.println(relativePath); // Output: ../../pictures

40
Q

How can we clean a path?

A

path.normalize()

Path path1 = Paths.get(“/home/./user/../john/./documents”);
Path normalizedPath1 = path1.normalize();
System.out.println(normalizedPath1); // Output: /home/john/documents

41
Q

How can we copy a file or directory?

A

Files.copy(Paths.get(“”), Paths.get(“”))
Files.copy(Paths.get(“”), Paths.get(“”), StandardCopyOption.REPLACE_EXISTING)
Files.copy(InputStream, OutputStream)

Shallow copy is done by default.

42
Q

How can we move a file or directory?

A

Files.move(Paths.get(“”), Paths.get(“”))
Files.move(Paths.get(“”), Paths.get(“”), StandardCopyOption.ATOMIC_MOVE)

43
Q

How can we delete file or directory?

A

Files.delete(Path.get(“”))
Files.deleteIfExists(Paths.get(“”))

44
Q

What do we need to watch out when using Files.delete()?

A

If we are deleting directories, they must be empty.
Syslinks are deleted instead their destinations.

45
Q

How can we check if two files are the same?

A

Files.isSameFile(path1, path2)
It can throw exception if there is no one of the files.

46
Q

How can we find out where two files are different?

A

Files.mismatch(path1, path2)

-1 if files are the same, 0 and above is index where they mismatch.

47
Q

How can we read whole text file?

A

Files.readString(path)

48
Q

How can we write string content to a file?

A

Files.writeString(path, string)

49
Q

How can we read file into memory?

A

Files.readAllBytes(input)

50
Q

How can we read all lines from file?

A

Files.readAllLines(input)

51
Q

How can we lazy read lines from file?

A

Stream<String> s = File.lines(path)</String>

52
Q

How can we find file at certain path?

A

Files.find(startPath, maxDepth, matcher, FileVisitOption.FOLLOW_LINKS).

Matcher is BiPredicate to set as a search criteria.

53
Q

How can we walk certain path?

A

Files.walk(path, maxDepth)

This goes depth first.

Caution because of the circular dependencies.

54
Q

How can we read files attributes?

A

BasicFileAttributes data = Files.readAttributes(path, BasicFileAttributes.class)

Can also be DosFileAttributes, PosixFileAttributes

55
Q

How can we update file attributes?

A

BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class)

56
Q

Files.isDirectory(path)

A

Checks if the given path is a directory.

57
Q

How can we check if a path represents a directory?

A

Files.isDirectory(Path path)

58
Q

Files.isSymbolicLink(Path path)

A

Tests if the file is a symbolic link.

59
Q

How can we determine if a file is a symbolic link?

A

Files.isSymbolicLink(Path path)

60
Q

Files.isHidden(Path path)

A

Tells whether the file is considered hidden.

61
Q

How can we determine if a file is hidden?

A

Files.isHidden(Path path)

62
Q

Files.isReadable(Path path)

A

Tests whether the file is readable

63
Q

How can we check if a file is readable?

A

Files.isReadable(Path path)

64
Q

Files.isWritable(Path path)

A

Tests whether the file is writable.

65
Q

How can we determine if a file is writable?

A

Files.isWritable(Path path)

66
Q

Files.isExecutable(Path path)

A

Tests whether the file is executable.

67
Q

How can we check if a file is executable?

A

Files.isExecutable(Path path)

68
Q

Serialization?

A

Class needs to implement Serializable interface.
Each member must be Serializable also, or transient or null.

69
Q

Deserialization?

A

Done trough ObjectInputStream and ByteArrayInputStream or FileInputStream. Then we can read with readObject().

readObject() is used with while(true) and catching EOFException to end read.

  1. Constructor of objects that is being deserialized is called. Then Constructor of the first ancestor that is not serializable is called, including Object
  2. Static and transient members are ignored
  3. values missing will be filled with null or 0
  4. If object has default values, and they are marked static or transient, then those values will stay
  5. Deserialization can trigger static init blocks
70
Q

What types of byte streams are there?

A

FileInputStream
FileOutputStream
BufferedInputStream
BufferedOutputStream
ObjectOutputStream
ObjectInputStream
PrintStream

71
Q

What types of char streams are there?

A

FileReader
FileWriter
BufferedReader
BufferedWriter
InputStreamReader
PrintWriter

72
Q

What system streams are there?

A

System.In
System.Err
System.Out

We must not close them!

73
Q

How can we read user input?

A

var reader = new BufferedReader(new InputStreamReader(System.In));
reader.readLine();

Console c = System.console();
c.readLine()

74
Q

How can we format text from console?

A

Console c = System.console();
c.format(“”);

75
Q

How can we read user input password?

A

Console c = System.console();
c.readPassword()

76
Q

How can we instantiate new BufferedWriter?

A

Files.newBufferedWriter(path)

76
Q

How can we instantiate new BufferedReader?

A

Files.newBufferedReader(path)

77
Q

How do we use InputStream?

A

We use read() method to read input. We iterate trough read until we get to -1, which is EOF

78
Q

How do we use OutpuStream?

A

We use write() method to write content.

79
Q

How do we use buffered InputStream?

A

“read” method works in a way that each iteration that it is called, it advances in reading its input. so we need to provide byte array with buffer, or chunk size. Then we must mark how much of the input have we read, and that is the output of read method. result of read method is how much data has been read that iteration. if it is 0 or smaller, we reached EOF. read method accepts byte array (buffer), offset (0) and size (chunk size).

int batchSize = 20; // Increased for demonstration
byte[] buffer = new byte[batchSize];
int lengthRead;
int iteration = 1;
while ((lengthRead = in.read(buffer, 0, batchSize)) > 0) {
out.write(buffer, 0, lengthRead);
}

80
Q

How can we mark bytes using InputStream?

A

We need to check if stream supports markation with “markSupported()”. Then we use mark(n) to mark index of a byte where we want to save position. Then, we can use reset() to go back to that position in stream.

We can use skip(n) to skip as many bytes we want.

81
Q

How do we use BufferedReader?

A

Reader reads text files, so we readLine() until we get null, which is EOF. readLine() returns text it has read from that line.

82
Q

How do we use BufferedWriter?

A

Writer writes with write() as long it has input. We must provide newLine() at the each iteration.