📁 File Handling, Iterator, Generics & Threads
File Handling, Iterator, Generics &: File handling means opening, using, and closing a resource (otherwise memory/handles leak), Iterator means traversing a collection in order w
File handling means opening, using, and closing a resource (otherwise memory/handles leak), Iterator means traversing a collection in order without knowing its internal structure, and Generics means running a single piece of code with different types in a type-safe way — like taking a document from a safe and locking it when done, a tour guide showing books in order without memorizing the shelf layout, and one template able to produce both a `List ` and a `List `. But if without Generics you could use `Object` and hold anything, why do you need a type parameter like `List `? Because with `Object` you can put the wrong type into the list and the error only explodes at runtime as a `ClassCastException`; Generics makes this impossible at compile time. This is the power of Java's static typing; Python's type hints are optional and not enforced at runtime, while TypeScript's generics are the model closest to Java. For a QA engineer all three provide reliability: if you do not close the resource when writing a test report to a file, handle leaks break tests over a long run, and Generics prevents passing the wrong type of data into a test-data util at compile time, avoiding insidious flaky bugs that surface at runtime.
Files API — write, read, check, delete
Micro Lab: Code practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Why Does the Difference Between Files.write() and Files.writeString(APPEND) Matter?
`Files.write(path, List.of(...))` creates the file if it doesn't exist, OVERWRITES it from scratch if it does — the right choice for STARTING a test report FRESH.
Files.writeString(path…
`Files.writeString(path, "...", APPEND)` adds to the END WITHOUT deleting existing content — this is for appending lines to a continuously growing log file.
Files.exists(path) and…
`Files.exists(path)` and `Files.deleteIfExists(path)` are called in sequence because trying to delete a file that does NOT exist (with `Files.delete()`) THROWS `NoSuchFileException` — `deleteIfExists` removes this risk.
Properties.load(new…
`Properties.load(new FileInputStream(...))` reads a `.properties` file as key-value PAIRS — the standard way to manage environment-dependent values like `base.url` in QA config WITHOUT hardcoding them.
Iterator — safe removal during iteration
Why Does list.remove() Throw ConcurrentModificationException but it.remove() Doesn't?