java 2 3
fdsa
fdsa
-
- 1 / 496
-
Lernkarten
What are the signatures of PrintStream/PrintWriter.format-methods?
- public PrintStream format(String format, Object args...)
- public PrintStream format(Locale loc, String format, Object args...)
- public PrintWriter format(String format, Object args...)
- public PrintWriter format(Locale loc, String format, Object args...)
What happens here?
System.out.format("Food: %d tons", 2.0);
IllegalFormatConversionException -> int expected, floating point provided -> use %f instead
Why is it unnecessary/ unrecommended to close SystemStreams (like System.out)?
The JVM creates and opens them for us -> closing makes them permantly unavailiable for all threads in the remainder of the programm
What happens here?:
try(var out = System.out) {}
System.out.println("Hello");
Prints nothing
What happens here?
try (var err = System.err) {}
System.err.println("Hello");
Prints nothing
What happens here?
var reader = new BufferedReader(new InputStreamReader(System.in));
try(reader) {}
String data = reader.readLine();
IOException at runtime -> most InputStream classes will throw an exception if you try to operate on a closed stream
Which exception is thrown by Files.deleteIfExists?
IOException
Whats the NIO2 replacement for java.io.File.class?
java.nio.file.Path
Do we make a difference in how we treat a instance if it is a file or directory in NIO2?
No
Does Path support symbolic links?
Yes
Does java.io.File support symbolic Links?
No
Can we create an instance of Path directly?
No, is a interface
Whats the syntax of the Path factory method?
public static Path of(String first, String... more)
Whats the characteristics of a relative / absolute path for the exam?
A absolute starts with / -> the root directory or with c: .. / d://
a relative would be f.e.: bird/parrot.png
What does the varags in Path.of do?
accepts additional path elements whcih will be combined and automatically be separated by the operation system-depending file separator
What are two ways to get a instance of Path?
Path.of and Paths.get
What are ways to get a Path instance from a Uri?
f.e.: URI a = new URI("file://iceman.txt");
Path b = Path.of(a);
Path c = Paths.get(a);
URI d = b.toUri();
How to get a Path via Filesystems? Whats the syntax?=
FileSystems.getDefault().getPath("/home/zooDirectory");
How to convert a Fileto a Path? How the other way around?
File file = new File("some.png");
Path path = file.toPath();
File backToFile = path.toFile();
Whats the enum value,type and inherited interface for not following symoblic links?
NOFOLLOW_LINKS , LinkOption, CopyOption/OpenOption
Whats the enum value,type and inherited interface for moving a file as atomic file system operation?
ATOMIC_MOVE, StandardCopyOption , CopyOption
Whats the enum value,type and inherited interface for coping existing attributes to new file?
COPY_ATTRIBUTES , StandardCopyOption , CopyOption
Whats the enum value,type and inherited interface for overwriting files if it already exist?
REPLACE_EXISTING , StandardCopyOption , CopyOption
Whats the enum value,type and inherited interface for appending to the end of a file if it is already open for write
APPEND, StandardOpenOption , OpenOption
Whats the enum value,type and inherited interface for creating a new file if it does not exist?
CREATE, StandardOpenOption, OpenOption
Whats the enum value,type and inherited interface for creating a new file if it does not exist or overwrite
CREATE_NEW, StandardOpenOption, OpenOption
Whats the enum value,type and inherited interface for opening for read access?
READ, StandardOpenOption, OpenOption
Whats the enum value,type and inherited interface for erase file and append to beginning if it is already open for write
TRUNCATE_EXISTING, StandardOpenOption, OpenOption
Whats the enum value,type and inherited interface for opening for write access?
WRITE, StandardOpenOption, OpenOption
Whats the enum value,type and inherited interface for following symbolic links?
FOLLOW_LINKS, FileVisitOption
Whats the default behaviour of Files.exists() for symbolicLinks?
checks the target of the smybolic link
Are Path instance mutable?
No, just like string-values
What happens here?
Path p = Path.of("whale");
p.resolve("krill");
System.out.print(p);
prints whale
Whats a rule of thumb when a method declares a IOException within NIO2?
When it requires the paths it operates on to exists
What happens if we call Path.getName with an invalid Index?
Throws an IllegalArgumentException at runtime
Are the references in Path.subpath(int beginIndex, int endIndex) inclusve or exclusive?
inclusive beginindex, exclusive endIndex
What happens with subpath if a invalid index is provided (also with getName(), subpath())
Throws a IllegalArgumentException
When does getParent() return null?
if operated on the root path or at the top of a relative path
What does getRoot return if the Path is a relative path?
Does not traverse outside the current working directory, returns null if it as the top of the relative path
Does getParent resolve pathSymbols like ".."?
No