7.1 I/O Stream in Java
In Java, an I/O stream (short
for Input/Output stream) is a fundamental concept used for reading data from
and writing data to various sources and destinations, such as files, network
connections, or memory. It provides a way to transfer data in a sequential and
consistent manner, regardless of the source or destination.
I/O streams are typically
categorized into two main types:
Input Streams: These streams
are used for reading data from a source. They allow you to retrieve data from
various sources, like files, keyboard input, or network sockets. Some common
classes that represent input streams include FileInputStream, BufferedInputStream, and
ObjectInputStream.
Output Streams: These streams
are used for writing data to a destination. They allow you to send data to
various destinations, like files, console output, or network sockets. Common
classes for output streams include FileOutputStream, BufferedOutputStream, and
ObjectOutputStream.
I/O streams can be further categorized based on the type
of data they handle:
● Byte Streams: These streams deal with raw binary data,
typically reading and writing data as bytes. InputStream and OutputStream are
the base classes for byte streams.
●
Character Streams: These
streams handle character data, typically in the form of text. They are used for
reading and writing characters in a character encoding-specific way. Reader and Writer are the base classes for
character streams.
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class FileCreateReadWrite {
public static void main(String[] args) {
try {
// Step 1: Create File
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
// Step 2: Write to File
try (FileOutputStream fos = new FileOutputStream(file)) {
String data = "Hello! This file is created and written using Java.";
fos.write(data.getBytes());
System.out.println("Data written successfully.");
}
// Step 3: Read from File
try (FileInputStream fis = new FileInputStream(file)) {
int ch;
System.out.println("Reading file content:");
while ((ch = fis.read()) != -1) {
System.out.print((char) ch);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java Applets
Java applets were small Java
programs designed to run within a web browser. They were a way to add
interactive and dynamic content to web pages in the 1990s and early 2000s. Java
applets provided a means to create animations, games, and other interactive
features in a web page, similar to how JavaScript is used today.
Key Features
-
Platform Independence:
Java applets could run on any system with a JVM, making them platform independent. -
Security Model:
They operated in a sandbox environment that restricted access to the system for security. -
Interactivity:
Applets allowed creation of interactive features like animations and games. -
Object-Oriented:
They were developed using Java, which supports object-oriented programming.
Limitations
-
Security Concerns:
Java applets had security vulnerabilities that made them unsafe over time. -
Browser Support Removed:
Modern browsers stopped supporting Java plugins required for applets. -
Performance Issues:
Applets were slow to load and run compared to modern technologies. -
Complexity:
They were difficult to develop and required additional setup to run.
Java Applet Program + HTML
Java Applet Code
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 50, 50);
}
}
HTML File to Run Applet
<html>
<head>
<title>Java Applet Example</title>
</head>
<body>
<h2>My First Java Applet</h2>
<applet code="HelloApplet.class" width="300" height="200">
</applet>
</body>
</html>
How It Works
-
First, the Java applet program is written and compiled to generate a
.classfile. - Then, an HTML file is created to embed the applet.
-
The
<applet>tag is used to load the compiled class file. - When the HTML file is opened, the browser automatically runs the applet.
-
The
paint()method is called and the output is displayed inside the applet window.
Keep in
mind that most modern web browsers will not support Java applets, and they may
even prompt users to enable deprecated plugins, which is a security risk. For
this reason, it's strongly recommended to use alternative web technologies like
JavaScript and HTML5 for creating interactive web content.
If you
need interactivity and dynamic content in your web application, consider using
JavaScript and HTML5, which are widely supported and more secure alternatives.
Comments
Post a Comment