Input and Output (I/O)
Programs aren't useful in a vacuum β they need to talk to the outside world. I/O is everything that crosses your program's boundary.
From Bee: Treat all external input as untrusted. Validate, parse, and sanitize before doing anything with it.
What Is I/O?
Input is data flowing into your program β keypresses, mouse clicks, file contents, network responses, sensor readings. Output is data leaving your program β text on the screen, files saved, HTTP responses, lights turning on. Every useful program has both.
Output: Print and Console.Log
The simplest output is printing text to the screen. Used constantly during development for debugging.
Input: Keyboard and Beyond
For learning purposes, reading a line of text from the user is the typical first input. Real programs read from files, databases, networks, sensors, and UI events.
Files
Reading and writing files lets your program persist data between runs. The basic flow is: open the file, read or write, close it.
Code Examples in Three Languages
// Output
console.log("Hello!");
// In Node.js β read line of input
const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin });
rl.question("Your name? ", (name) => {
console.log("Hello, " + name);
rl.close();
});
// File
const fs = require("fs");
fs.writeFileSync("hello.txt", "saved!");
const contents = fs.readFileSync("hello.txt", "utf8");# Output
print("Hello!")
# Input
name = input("Your name? ")
print("Hello, " + name)
# File
with open("hello.txt", "w") as f:
f.write("saved!")
with open("hello.txt", "r") as f:
contents = f.read()import java.util.Scanner;
import java.io.*;
// Output
System.out.println("Hello!");
// Input
Scanner sc = new Scanner(System.in);
System.out.print("Your name? ");
String name = sc.nextLine();
// File
FileWriter fw = new FileWriter("hello.txt");
fw.write("saved!");
fw.close();Best Practices
- Always close files when done (or use language constructs that auto-close).
- Validate user input β it's the most common source of bugs.
- Don't mix concerns β keep I/O separate from business logic for testability.
- Use buffered I/O for large files.
Common Mistakes
- Forgetting to close a file. Resource leaks add up.
- Trusting user input. Treat all external input as suspect.
- Hard-coding paths. Use config or relative paths.
- Reading huge files all at once. Stream them instead.
We have a dedicated Common Bugs with Input Output page β five real broken snippets with the fix. Read it before you start writing.
How It Works Under the Hood
I/O is fundamentally slower than computation β by orders of magnitude. Reading from disk is ~100,000Γ slower than reading from RAM; reading from network is another ~100Γ. This is why every language wraps I/O in buffering (read in chunks, not bytes) and modern languages add async primitives so your CPU can do other work while bytes travel.
Standard streams β stdin, stdout, stderr β are file descriptors 0, 1, 2 in Unix. Redirecting > log.txt reroutes stdout. 2>&1 redirects stderr to wherever stdout is going.
Closing files matters: every open file consumes an OS resource. Long-running programs that leak handles will eventually be denied new opens. This is why with open(...) in Python and try-with-resources in Java exist β to guarantee close.
From Beginner to Pro
The same concept gets deeper as you grow. Here's what mastery looks like at three levels:
You print to console, read from a file, ask for user input.
You stream large files line-by-line, parse JSON safely, validate input, and use proper resource cleanup.
You async-await for I/O, handle backpressure on streams, log to stderr separately from stdout, and treat all external input as untrusted by default.
Performance & Gotchas
- Trusting form input. Always validate; never concatenate into queries.
- Reading huge files all at once β out of memory.
- Forgetting to close files β handle leaks.
- Mixing stdout and stderr when redirecting.
Authoritative references: MDN β Fetch API, Python β I/O, Oracle β Java I/O, OWASP Top 10 β input validation.
Quick Quiz
Real-World Uses (Production Code, Today)
Web forms
Every form submit is input. Every "Thank you for signing up" page is output. The full web reduces to this.
Log files
Production servers write logs (output) and read config files (input) constantly. Debugging often means reading the right log.
REST APIs
A request body is input. The JSON response is output. Same input β process β output triangle, scaled to millions of requests/sec.
Frequently Asked Questions
What's the difference between print and console.log?
Both write text. print in Python, console.log in JavaScript, System.out.println in Java. Same idea.
How do I read a file line by line?
Iterate over the file object: for line in open("f.txt") in Python.
What's stdin/stdout?
Standard input and output β the default streams a program reads from and writes to.
How do I get user input in JavaScript in a browser?
Use a form element or prompt(). prompt is fine for learning, not production.
What is "stderr"?
Standard error β a separate output stream for errors. Lets you redirect logs and errors independently.
How do I parse JSON input?
JSON.parse(text) in JS, json.loads(text) in Python.
What's a "buffer"?
Memory that holds chunks of data while it's being read or written. Buffering avoids one-byte-at-a-time slowness.
Why does my program freeze waiting for input?
Reading input blocks until the user types something. That's usually intended.
Related Concepts
Data Types in Programming
Related concept.
Strings
Related concept.
Operators
Related concept.
- Input and Output (I/O) is one of the 13 universal concepts of programming.
- The syntax differs across languages, but the underlying idea is the same.
- Practice in the playground to make it stick.