JAVA — CHAPTER 11

Files, I/O Streams, JSON Serialization & CSV Files · Cheat Sheet
Files & Streams NIO Text Files JSON CSV RSA Basics
1 FILES & STREAMS — THE BIG PICTURE

What's a stream?

  • An ordered sequence of bytes/characters flowing between a program and a source/destination
  • Byte streams — raw data (images, any file) — e.g. FileInputStream
  • Character streams — text, handles encoding — e.g. FileReader

The File class

  • Represents a path on disk — does not hold file contents
  • exists(), isDirectory(), length(), delete()
2 MODERN FILE ACCESS WITH NIO
Path p = Paths.get("data.txt"); System.out.println(Files.size(p)); System.out.println(Files.isDirectory(p)); List<String> lines = Files.readAllLines(p);

Why NIO ("New I/O")

java.nio.file gives a cleaner, more powerful API (Path, Files) than the classic java.io.File for querying metadata, walking directory trees, and doing bulk reads/writes.

3 SEQUENTIAL TEXT FILES

Writing

try (PrintWriter out = new PrintWriter("out.txt")) { out.println("Hello, file!"); }

Reading

try (Scanner in = new Scanner(new File("out.txt"))) { while (in.hasNextLine()) System.out.println(in.nextLine()); }
4 JSON SERIALIZATION
// JSON representation of an object { "name": "Amit", "age": 21, "active": true }

Key ideas

  • Serialization — converting a Java object into JSON text
  • Deserialization — parsing JSON text back into a Java object
  • Libraries like Jackson/Gson do this automatically via annotations/reflection
  • JSON is the standard format for web-service data exchange
5 PROCESSING A JSON WEB-SERVICE RESPONSE

Typical flow: send an HTTP request to a REST API → receive a JSON response body (as text) → deserialize it into Java objects → work with the data like any other object.

HttpClient client = HttpClient.newHttpClient(); HttpRequest req = HttpRequest.newBuilder(URI.create(url)).build(); HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString()); // resp.body() holds the raw JSON text to parse
6 CSV FILES — CREATE, READ & ANALYZE

What is CSV?

"Comma-Separated Values" — a plain-text tabular format; each line is a row, fields separated by commas. Simple to write/read with split(",").

try (Scanner sc = new Scanner(new File("titanic.csv"))) { sc.nextLine(); // skip header row while (sc.hasNextLine()) { String[] fields = sc.nextLine().split(","); // analyze survival rate, age groups, etc. } }
7 SECURITY CASE STUDY: RSA PUBLIC-KEY CRYPTOGRAPHY

Core idea

Each user has a public key (shared openly, used to encrypt) and a private key (kept secret, used to decrypt). Anyone can encrypt a message for you, but only you can decrypt it.

Where it's used

  • Secure web traffic (HTTPS/TLS)
  • Digital signatures
  • Underlies secure file transfer & messaging
8 METHOD REFERENCE TABLE

class Files (java.nio.file)

MethodPurpose
exists(path)true if the path exists
size(path)file size in bytes
readAllLines(path)List<String> of every line
write(path, bytes)writes data to the file
isDirectory(path)true if it's a folder
delete(path)removes the file/folder

PrintWriter & Scanner (file I/O)

MethodPurpose
println(x)writes x + newline to the file
printf(fmt, args)writes formatted output
close()flushes & closes the writer
hasNextLine()true if more lines remain to read
nextLine()reads the next full line