Java Input/Output (I/O) Tutorial
Master Java input/output operations: Learn to read user input, write to console, handle files, and format output with practical examples using Scanner, BufferedReader, and System.out methods.
1. Introduction to Java I/O
Input/Output (I/O) lets programs communicate with users and external systems. Java provides classes in java.io and java.util for console and file operations.
- Output: System.out for console display
- Input: Scanner and BufferedReader for keyboard
- Streams abstract byte and character data flow
- Always close resources or use try-with-resources
import java.util.Scanner;
public class IOIntro {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name);
sc.close();
}
}
2. Console Output Methods
System.out is a PrintStream used for standard output. println adds a newline; print stays on the same line; printf formats values like C-style printf.
- System.out.println() — print with newline
- System.out.print() — print without newline
- System.out.printf() — formatted output
- System.err — separate stream for error messages
public class OutputDemo {
public static void main(String[] args) {
System.out.print("Name: ");
System.out.println("Alice");
System.out.printf("Score: %.1f%n", 92.5);
}
}
3. Reading Input with Scanner
Scanner parses primitive types and strings from readable sources. It is beginner-friendly but can leave newline characters in the buffer when mixing nextInt with nextLine.
- nextInt(), nextDouble(), nextLine() for common types
- Use hasNext() to check before reading
- Call nextLine() after nextInt() to consume leftover newline
- Close Scanner when done
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
sc.nextLine();
System.out.print("Enter city: ");
String city = sc.nextLine();
System.out.println(age + " from " + city);
sc.close();
}
}
4. Reading with BufferedReader
BufferedReader reads text efficiently through a character buffer. It is preferred for reading large text input and integrates with try-with-resources.
- Wrap InputStreamReader around System.in
- readLine() returns null at end of stream
- More efficient than Scanner for line-based input
- Use with try-with-resources for auto-close
import java.io.*;
public class ReaderDemo {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(System.in))) {
System.out.print("Enter text: ");
String line = br.readLine();
System.out.println("You typed: " + line);
}
}
}
5. File I/O Basics
Files store persistent data on disk. Java offers character streams (Reader/Writer) for text and byte streams (InputStream/OutputStream) for binary data.
- File class checks existence and paths
- FileReader/FileWriter for text files
- Always handle IOException
- Prefer NIO Files API in modern Java
import java.io.*;
public class FileWriteDemo {
public static void main(String[] args) throws IOException {
try (FileWriter fw = new FileWriter("notes.txt")) {
fw.write("Java file I/O works!");
}
}
}
6. Practical I/O Examples
Combining input validation with formatted output is a common pattern in console applications. Always validate user input before processing.
- Menu-driven programs use Scanner in a loop
- Parse numeric input with exception handling
- Format currency with printf
- Log errors to System.err
import java.util.Scanner;
public class ValidInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
if (sc.hasNextInt()) {
int n = sc.nextInt();
System.out.println("Double: " + (n * 2));
} else {
System.out.println("Invalid input");
}
sc.close();
}
}