MODULE 1 · TOPIC 10 · VISUAL PROGRAMMING LAB

Input/Output in Java

Watch input, files, streams and resource handling through virtual devices.

Browser-based simulations: change the inputs, trace the Java statements and inspect results. No arbitrary Java execution, real database, threads or file changes occur here.

1. Scanner — parse console input

Send text into the program and inspect token conversion.

Java example
Program / simulation outputConsole
Live state / data blocksCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Scanner splits input into tokens and parses types. hasNextInt checks before consuming. This demo uses a string instead of System.in and plain ASCII integer tokens.

Try and observe

Try 42, -5, hello and 12 20.

2. Write and read a text file

Observe virtual file contents and resource handling.

Java example
Program / simulation outputConsole
Live state / data blocksCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Files.writeString and readString use UTF-8 by default. APPEND preserves previous content; TRUNCATE_EXISTING replaces it. The browser does not alter your disk.

Try and observe

Try append and replace with same message.

3. Bytes and characters — UTF-8

See how text becomes a sequence of bytes.

Java example
Program / simulation outputConsole
Memory / data inspectorCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

InputStream/OutputStream process bytes, Reader/Writer process characters. Use a consistent character encoding; UTF-8 byte count differs from String.length().

Try and observe

Compare Java and Tamil or emoji.

4. Hands-on practice and assessment

Programming challenge

Read a line with Scanner(System.in), write it to a UTF-8 file, then read it back. Handle IOException.

Edit the scratchpad below. Copy your code into an actual Java project or IDE to compile and test it.

Quick assessment

Key points and real Java notes

Use try-with-resources for streams and readers. Avoid closing a shared System.in if another part of the application still needs it. Use Path and Files for modern file operations.