OCP 17 Exam - chapter 14 notes - I/O
Streams
byte streams are called
*InputStream/*OutputStream, where character streams are*Reader/*Writerexceptions are
PrintWriterandPrintStreamexam my trick us by wrapping
Writer/ReaderintoInput/OutputStreamor wrappingInputStreamintoOutputStreambe aware that not all readers support
mark(int), so program might for some readers throwIOException
IO
- it might be tricky that
Filehas constructor with parent and child, e.g.new File(new File("/root"), "/user/.bashrc")(Q.15)
NIO.2
memorize creating
Path:Path.of,Paths.get,FileSystem.getPath(Q.10)be aware that
Path#toRealPathmight throwNoSuchFileException(Q.23)Files.readAllLinesreturnsList, whileFiles.linesreturnsStream(Q.8)be aware that most of
Filesmethods throwsIOException, so questions might hide compilation error whenIOExceptionis not handled (Q.4)both methods
Files.walkandFiles.findreturnsStream<Path>, but forFiles.findwe can provide path and attribute filter as method parameter (Q.5)to create directory we should use
Files.createDirectory(to avoid exception when no parent, useFiles.createDirectories)
System console
System.consolemight returnnullif not available (Q.3)
Playground code
https://github.com/RG9/rg-playground-ocp17/tree/main/Chapter14/src/test/java/pl/rg9/demo
Credit: OCP Oracle Certified Professional Java SE 17 Developer Study Guide