Java IO Overview
Java IO Overview
In Java, the `java.io` package is part of the standard library that provides input and output (I/O)
functionality, allowing programs to read data from sources (input) and write data to destinations
(output). These sources and destinations can be files, memory, keyboards, consoles, or network
connections.
🔹 Overview of `java.io`
🔹 Key Concepts
1. Streams
Streams represent sequences of data. Java uses streams to perform input and output:
✅ File Class
Used to represent file and directory paths.
java
✅ FileInputStream / FileOutputStream
For reading and writing binary data from/to files.
java
✅ FileReader / FileWriter
Simplifies reading and writing character files.
java
✅ BufferedReader / BufferedWriter
Buffers input/output for efficiency (reads/writes large chunks at a time).
java
✅ PrintWriter
Easier way to write formatted data.
java
✅ ObjectInputStream / ObjectOutputStream
Used for object serialization (reading/writing entire objects).
java
🔹 Exception Handling
Most I/O classes throw checked exceptions, especially `IOException`. You must handle them using
`try-catch` or declare them with `throws`.
java
try {
FileReader fr = new FileReader("input.txt");
} catch (IOException e) {
e.printStackTrace();
}
🔹 Summary Diagram
mathematica
Input Output
------------------- ---------------------
InputStream (bytes) OutputStream (bytes)
| |
FileInputStream FileOutputStream
BufferedInputStream BufferedOutputStream
If you want, I can also give you short practice questions or coding examples for any specific class like
`BufferedReader` or `FileWriter`.
Method Purpose
`init()` Called once when the applet is first loaded
`start()` Called every time the applet becomes active (e.g., user visits page)
`paint(Graphics g)` Called to draw the applet’s content on the screen
`stop()` Called when the applet is no longer active (e.g., user leaves page)
`destroy()` Called when the applet is being removed from memory
java
import java.applet.Applet;
import java.awt.Graphics;
html
<html>
<body>
<applet code="AppletDemo.class" width="300" height="100">
Your browser does not support applets.
</applet>
</body>
</html>
➤ How to Run:
nginx
javac AppletDemo.java
nginx
appletviewer filename.html
Note: Modern browsers have dropped support for Java Applets, so applet code will not run
directly in them anymore.
🔹 Current Status
Java Applets are obsolete.
Use Java Web Start (also deprecated) or modern technologies like:
JavaFX (for desktop apps)
JavaScript + HTML5 (for web apps)
Spring Boot, JSP, or other frameworks for server-side Java
🔹 Summary
Feature Details
Runs in Browser / Applet viewer
Base class `Applet` or `JApplet`
GUI components AWT / Swing
Lifecycle methods `init()`, `start()`, `paint()`, `stop()`, `destroy()`
Status Deprecated
If you want, I can help you write a simple GUI-based applet with buttons or text fields for better
understanding.
Java Swing: Creating a Swing Applet and Application, Programming using Panes, Pluggable Look
and feel. Labels. Text fields, Buttons, Toggle buttons, Checkboxes, Radio Buttons, View ports, Scroll
Panes, Scroll Bars, Lists, Combo box, Progress Bar, Menus and Toolbars, Layered Panes, Tabbed
Panes, Split Panes, Layouts, Windows, Dialog Boxes. Explain with code
Here's a comprehensive explanation of Java Swing covering how to create Swing Applets and
Applications, use common GUI components, manage layouts, apply pluggable look and feel, and
more — with simple code examples for each.
import javax.swing.*;
frame.setVisible(true);
}
}
java
import javax.swing.*;
import java.awt.*;
✅ Progress Bar
java
✅ Layered Pane
java
layeredPane.add(background, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(foreground, JLayeredPane.PALETTE_LAYER);
✅ Tabbed Pane
java
✅ Split Pane
java
file.add(open);
menuBar.add(file);
frame.setJMenuBar(menuBar);
✅ Toolbar
java
🧱 7. Layout Managers
Layout Purpose
`FlowLayout` Default for JPanel, adds left to right
java
frame.setLayout(new BorderLayout());
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("Center"), BorderLayout.CENTER);
✅ Input Dialog
java
✅ Confirm Dialog
java
java
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// or "javax.swing.plaf.metal.MetalLookAndFeel"
} catch (Exception e) {
e.printStackTrace();
}
🔚 Summary Table
Component Class Name
Label `JLabel`
Text Field `JTextField`
Button `JButton`
Toggle Button `JToggleButton`
Checkbox `JCheckBox`
Radio Button `JRadioButton`
List `JList`
Combo Box `JComboBox`
Toolbar `JToolBar`
Tabbed Pane `JTabbedPane`
Let me know if you want a complete project example combining several components in one app.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/1