I/O and Java Applets – Java Programming Notes
I/O and Java Applets are important topics in Java programming. Java I/O allows programs to receive input and produce output using the keyboard, console, files and other sources. Java Applets were small Java programs designed to run inside an applet-compatible environment.
In these notes, students will learn about Java input/output streams, System.in, System.out, System.err, byte streams, character streams, file handling, and Java Applet lifecycle methods with simple Java examples.
What is I/O in Java?
I/O stands for Input and Output. Java I/O provides different classes and methods that allow Java programs to receive information from a source and send information to a destination.
Input
Input means data entering the Java program. Input may come from:
- Keyboard
- File
- Network
- Memory
- Other input devices
Output
Output means data produced by a Java program. It can be sent to:
- Computer screen
- File
- Network
- Memory
- Other output devices
Many traditional Java input/output classes are available through the
java.io package.
Java Streams
A stream is a flow of data between a source and a destination. Java uses streams to perform input and output operations.
Standard Streams in Java
Java provides three commonly used standard streams:
| Stream | Purpose |
|---|---|
System.in |
Standard input stream |
System.out |
Standard output stream |
System.err |
Standard error output stream |
1. System.in
System.in is normally used to receive input from the keyboard.
It is an InputStream.
import java.io.IOException;
public class SystemInExample {
public static void main(String[] args) throws IOException {
System.out.println("Enter a character:");
int data = System.in.read();
System.out.println("You entered: " + (char)data);
System.out.println("Character value: " + data);
}
}
Some methods associated with input streams include:
read()read(byte[] b)available()close()
Taking Input Using Scanner
For beginners, Java’s Scanner class provides a convenient way of
taking keyboard input.
import java.util.Scanner;
public class StudentInput {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter your age: ");
int age = input.nextInt();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
input.close();
}
}
2. System.out
System.out is used to display normal output on the computer screen.
Common methods are:
print()println()printf()
print()
The print() method prints output without automatically moving the
cursor to the next line.
public class PrintExample {
public static void main(String[] args) {
System.out.print("Nepal ");
System.out.print("eNotes");
}
}
Output:
Nepal eNotes
println()
The println() method prints the output and then moves the cursor
to the next line.
public class PrintlnExample {
public static void main(String[] args) {
System.out.println("Java");
System.out.println("Programming");
}
}
Output:
Java
Programming
printf()
The printf() method is useful when formatted output is required.
public class PrintfExample {
public static void main(String[] args) {
int marks = 85;
double percentage = 85.4567;
System.out.printf("Marks = %d%n", marks);
System.out.printf("Percentage = %.2f%%%n", percentage);
}
}
Output:
Marks = 85
Percentage = 85.46%
3. System.err
System.err is primarily used to display error or diagnostic messages.
public class ErrorExample {
public static void main(String[] args) {
System.err.println("Error: Invalid input.");
}
}
Types of Streams
Streams can first be divided according to direction:
- Input Stream
- Output Stream
Input Stream
Input streams read data from a source and transfer it into a Java program.
Examples:
- FileInputStream
- BufferedInputStream
- DataInputStream
- ByteArrayInputStream
Output Stream
Output streams transfer data from a Java program to a destination.
Examples:
- FileOutputStream
- BufferedOutputStream
- DataOutputStream
- ByteArrayOutputStream
Byte Streams
Byte streams work mainly with raw 8-bit bytes. They are useful for working with binary data.
Examples of binary data include:
- Images
- Audio files
- Video files
- Binary documents
The major base classes are:
InputStreamOutputStream
Common Byte Stream Classes
| Class | Purpose |
|---|---|
| FileInputStream | Reads bytes from a file |
| FileOutputStream | Writes bytes into a file |
| BufferedInputStream | Provides buffered byte input |
| BufferedOutputStream | Provides buffered byte output |
| DataInputStream | Reads Java primitive data types |
| DataOutputStream | Writes Java primitive data types |
Copying a File Using Byte Stream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) throws IOException {
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream("source.txt");
output = new FileOutputStream("destination.txt");
int data;
while ((data = input.read()) != -1) {
output.write(data);
}
System.out.println("File copied successfully.");
}
finally {
if (input != null)
input.close();
if (output != null)
output.close();
}
}
}
Character Streams
Character streams are mainly designed for text data. They work with characters instead of raw byte-oriented data.
The main abstract classes are:
ReaderWriter
Common Character Stream Classes
| Class | Purpose |
|---|---|
| FileReader | Reads characters from a text file |
| FileWriter | Writes characters into a text file |
| BufferedReader | Provides buffered character input |
| BufferedWriter | Provides buffered character output |
| InputStreamReader | Converts bytes into characters |
| OutputStreamWriter | Converts characters into bytes |
| PrintWriter | Provides convenient text output methods |
Reading a Text File
import java.io.FileReader;
import java.io.IOException;
public class ReadTextFile {
public static void main(String[] args) throws IOException {
FileReader reader = null;
try {
reader = new FileReader("notes.txt");
int data;
while ((data = reader.read()) != -1) {
System.out.print((char)data);
}
}
finally {
if (reader != null)
reader.close();
}
}
}
Byte Stream vs Character Stream
| Byte Stream | Character Stream |
|---|---|
| Works mainly with bytes | Works with characters |
| Suitable for binary data | Suitable for text data |
| Uses InputStream and OutputStream | Uses Reader and Writer |
| Can handle images and audio | Designed for textual information |
| Example: FileInputStream | Example: FileReader |
File Handling in Java
File handling allows Java programs to save information permanently and retrieve that information later.
A Java program can perform operations such as:
- Create files
- Write data
- Read data
- Append information
- Check file properties
- Delete files
Writing Data to a File
import java.io.FileWriter;
import java.io.IOException;
public class WriteFile {
public static void main(String[] args) throws IOException {
FileWriter writer =
new FileWriter("student.txt");
writer.write(
"Welcome to Java Programming"
);
writer.close();
System.out.println(
"Data written successfully."
);
}
}
Reading Data from a File
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
FileReader reader =
new FileReader("student.txt");
int data;
while ((data = reader.read()) != -1) {
System.out.print((char)data);
}
reader.close();
}
}
BufferedReader Example
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class BufferedInputExample {
public static void main(String[] args)
throws IOException {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(System.in)
);
System.out.print("Enter your name: ");
String name = reader.readLine();
System.out.println(
"Welcome, " + name
);
}
}
What is a Java Applet?
A Java Applet is a small Java program that was historically designed to execute within a Java-compatible browser environment or an Applet Viewer.
Unlike a normal Java application, a traditional applet normally did not start
through a standard main() method. Instead, its execution was
controlled using lifecycle methods.
Java Application vs Java Applet
| Java Application | Java Applet |
|---|---|
| Standalone program | Historically executed inside an applet environment |
| Usually starts using main() | Uses applet lifecycle methods |
| Runs directly using JVM | Traditionally required Applet Viewer or compatible browser technology |
| Used for general Java programs | Mostly studied as legacy Java technology |
Java Applet Life Cycle
The important traditional Applet methods are:
init()start()paint()stop()destroy()
1. init()
The init() method performs initial setup when the applet is loaded.
public void init() {
// Initialization code
}
2. start()
The start() method runs when the applet becomes active.
public void start() {
// Start applet operations
}
3. paint()
The paint() method is traditionally used for displaying text and
graphics.
public void paint(Graphics g) {
g.drawString(
"Welcome to Java Applet",
50,
50
);
}
4. stop()
The stop() method is called when the applet becomes inactive.
public void stop() {
// Stop temporary operations
}
5. destroy()
The destroy() method is used for final cleanup before the applet
is permanently removed.
public void destroy() {
// Final cleanup
}
Complete Java Applet Example
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void init() {
// Initialization
}
public void start() {
// Applet starts
}
public void paint(Graphics g) {
g.drawString(
"Welcome to Java Applet",
50,
50
);
}
public void stop() {
// Applet stops
}
public void destroy() {
// Cleanup
}
}
Drawing Graphics in Java Applet
Traditional Java Applets could use the Graphics class to draw
different shapes.
Common methods include:
drawString()drawLine()drawRect()fillRect()drawOval()fillOval()
import java.applet.Applet;
import java.awt.Graphics;
public class GraphicsApplet extends Applet {
public void paint(Graphics g) {
g.drawString(
"Java Graphics",
50,
40
);
g.drawLine(
50,
60,
220,
60
);
g.drawRect(
50,
80,
120,
60
);
g.drawOval(
50,
170,
120,
60
);
}
}
Important Points to Remember
- I/O means Input and Output.
-
java.iocontains many traditional Java I/O classes. -
System.inis used for standard input. -
System.outis used for normal standard output. -
System.erris used for error output. - Byte streams are suitable for binary information.
- Character streams are useful for text.
-
FileInputStreamreads bytes from files. -
FileOutputStreamwrites bytes to files. -
FileReaderreads character data. -
FileWriterwrites character data. -
The major traditional Applet lifecycle methods are
init(),start(),paint(),stop()anddestroy(). - Java Applets are no longer supported by modern mainstream browsers.
Important Exam Questions
- What is Input and Output in Java?
- Define a stream in Java.
- What is System.in?
- Explain System.out.
- What is System.err?
- Differentiate between print() and println().
- Explain printf() with an example.
- What is an input stream?
- What is an output stream?
- What is a byte stream?
- What is a character stream?
- Differentiate between byte stream and character stream.
- What is FileInputStream?
- What is FileOutputStream?
- Explain FileReader and FileWriter.
- What is BufferedReader?
- Write a Java program to read data from a file.
- Write a Java program to write data into a file.
- What is a Java Applet?
- Differentiate between Java Application and Java Applet.
- Explain the life cycle of a Java Applet.
- What is the purpose of init()?
- What is the purpose of paint()?
- Explain start(), stop() and destroy().
- Write a simple Java Applet program.
Frequently Asked Questions
System.in, System.out and System.err.
FileInputStream,
FileOutputStream, FileReader and
FileWriter.
init(),
start(), paint(), stop() and
destroy().
Final Words
Java I/O is an essential part of Java programming because it allows applications to communicate with keyboards, files and other sources using input and output streams.
Students should understand the difference between
byte streams and character streams and become familiar with
classes such as FileInputStream, FileOutputStream,
FileReader, FileWriter and
BufferedReader.
Java Applets are mainly an academic and historical topic today, but learning their structure and lifecycle provides useful knowledge about earlier Java GUI and web-programming concepts.
Use these I/O and Java Applets Notes for study, revision, practical programming and examination preparation.
Discussion
Share a helpful question, idea, or explanation with other students.