java 2 3
fdsa
fdsa
Set of flashcards Details
Flashcards | 496 |
---|---|
Language | Deutsch |
Category | Computer Science |
Level | Other |
Created / Updated | 06.12.2020 / 24.01.2021 |
Weblink |
https://card2brain.ch/box/20201206_java_2_3
|
Embed |
<iframe src="https://card2brain.ch/box/20201206_java_2_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
What happens with createDirectories if the directories already exists?
Will simply doe nothing -> createDirectory would throw an excetion
What happens with createDirectory if it already exists? What if the path leading up tp the directory does not exists?
throw an exception
What is the signature of Files.copy?
public static Path copy(Path source, Path target, CopyOption... options) throws IOException
Whats the default behavoiur of copy if the target already exists?
Throws an exception
What happens here if food.txt and /enclosure exists?
var file = Paths.get("food.txt");
var directory = Paths.get("/enclosure");
Files.copy(file, directory);
Throws an exception -> enclosure already exists -> Path would have to be "/enclosure/food.txt"
What happens with Files.delete if a directory is not empty?
Throws an exception
What happens if delete/deleteIf is called on a symbolic link?
The link will be deleted, not the target
What does newBufferedReader() do?
Reads the file specified at the Path location
What does Files.newBufferedWriter(Path path) do?
Writes to a file specified at the Path location
What is the signature of Files.readallLines?
public static List<String> readAllLines(Path path) throws IOException
Whats the problem with Files.readAllLines()?
May trigger an outofMemoryError
What is a regular file?
One that contains content -> no symbolic links
Does Files.isRegularFile check the target of symbolicLinks?
Yes
Which of these functions in Files will throw an exceiption if the path does not exist?
- isHedden, isReadable, isWritable, isExecutable
isHidden
What happens with Files.size on directories?
is undefined, depends on the filesystem
Whats the difference between BasicFileAttributes and BasicFileAttributeView?
BasicFileAttributes are readOnly, BasicFileAttributeView is also updatable
Whats the signature of readAttributes?
public static <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException
Signature of Files.getFileAttributeView?
public static <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
Can all file attributes be changed?
No, f.e. you can't change a file to a directory
Whats the signature of the File.list method?
public static Stream<Path> list(Path dir) throws IOException
Are symbolic links follow when using Files.list?
No
How deep traverses Files.list()?
only a single directory
Is Files.walk breadth-first or depth-first?
depth-first
Syntax of Files.walk()?
- public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException
- public static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException
Is Files.walk lazy?
Yes
Why is the getSize method outsourced here?
private long getSize(Path p) {
try{return Files.size(p);} catch(IOException e) {}
return 0L;
}
public long getPathSize(Path source) throws IOException {
try(var s= Files.walk(source)) {
return s.parallel().filter(p-> !Files.isDirectory(p))
.mapToLong(this::getSize)
.sum();
}
}
Files.size() declares IOException and we'd rather not put a try/catch block inside a lambda expression
Whats the minimum depth for Files.walk to get a non-zero result?
at least 1
Does Files.walk traverse symbolic links by default?
No
How to follow symbolic links with Files.walk?
try(var s = Files.walk(source, FileVisitOption.FOLLOW_LINKS))..
What happens if Files.walk follows symbolic links and there is a link to root?
Every file in the system will be searched
What happens with Files.walk if it follows symbolic links but a Path is visited twice?
It will throw a FileSystemLoopException
Whats the syntax of Files.find?
public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException
Does Files.find follow symbolic links by default?
No, but supports FOLLOW_LINK like walk
Whats a alternative for Files.readAllLines()?
File.lines()
What is the syntax of Files.lines()?
public static Stream<String> lines(Path path) throws IOException
Whats the definition/syntax of the interface runable?
@FunctionalInterface public interface Runnable {
void run();
}
In which thread will a runable be run=
Separate from the main application thread
How to pass information to runnables?
Implement the interface Runnable and pass information to the constructor
How to execute a Task with the Thread interface?
Define the thread with the corresponding task then start the Thread via Thread.start()
How to define the Task that a thread instance will execute?
Either:
- provide a runnable object or lambda expression to the Thread constructor
- create a class that extends Thread and overrides the run() method