0% found this document useful (0 votes)
12 views

Unit VII IOand Java Applet

The document covers I/O streams in Java, explaining input and output streams, and provides a Java program to read student data and store it in a file. It also discusses applets, their lifecycle, and includes example applet programs and their integration into HTML. Additionally, it differentiates between applets and standalone Java applications.

Uploaded by

pawanpoudel69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit VII IOand Java Applet

The document covers I/O streams in Java, explaining input and output streams, and provides a Java program to read student data and store it in a file. It also discusses applets, their lifecycle, and includes example applet programs and their integration into HTML. Additionally, it differentiates between applets and standalone Java applications.

Uploaded by

pawanpoudel69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Prepared by Manish ojha MCA

Unit VII I/O and Java Applets


7.1 I/O Stream
7.2 Read and Write Console
7.3 Concept of Applets
7.4 Applets in HTML file

Stream: A stream is a sequence of bytes from a source to destination


over a communication path. Java IO packaged is used.

There are basically two types of streams.

a. Input stream
b. Output stream
a. Input Stream (Java program point of view): The stream through
which the reading the value into java application from the file is
called input stream. it is used to reading the data
b. Output stream (Java program point of view) : The stream through
which the value from java application to stored in the file is called
output file. It is used to writing the data.
String.getBytes( ): returns the byte array.
FileOutputStream.flush( ): is used to clear the output stream buffer.
FileOutputStream.close( ): is used to close output stream(Close the
file)
1. Write a Java program to read the student data from user and
store it in the file.

import java.util.*;

import java.io.*;

class Readdata

Unit VII Programming in Java 1


Prepared by Manish ojha MCA

public static void main(String args[])

try

Scanner sc = new Scanner(System.in);

//reading content from file

String name;

int rollno;

//read text from file

System.out.println("Enter Name : ");

name = sc.next();

System.out.println("Enter Roll No : ");

rollno = sc.nextInt();

String r = String.valueOf(rollno);

FileWriter fw = new FileWriter("student.txt");

fw.write(name+" ");

fw.write(r);

fw.close();

catch(Exception e)

Unit VII Programming in Java 2


Prepared by Manish ojha MCA

System.out.println(e.getMessage());

Concept of Applet : Applet are small java applications that can be


accessed in an internet server.

Features:

a. An applet doesn't have any main ( ) method.


b. It is viewed using JVM.

Life cycle of an applet:

As shown in the above diagram, the life cycle of an applet starts


with init()method and ends with destroy() method. Other life cycle
methods are start(), stop() and paint(). The methods to execute only once

Unit VII Programming in Java 3


Prepared by Manish ojha MCA

in the applet life cycle are init() and destroy(). Other methods execute
multiple times.

Example program that demonstrates the life cycle of an


applet is as follows
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
public void init()
{
System.out.println("Applet initialized");
}
public void start()
{
System.out.println("Applet execution started");
}
public void stop()
{
System.out.println("Applet execution stopped");
}
public void paint(Graphics g)
{
System.out.println("Painting...");
}
public void destroy()
{
System.out.println("Applet destroyed");
}
}
A sample Program
Java file Code//first.java

import java.applet.Applet;

import java.awt.Graphics;

public class First extends Applet {

Unit VII Programming in Java 4


Prepared by Manish ojha MCA

public void paint(Graphics g)

g.drawString("WELCOME",150,150);

} }

HTML File Code:

First.html

Embedding Applet to HTML File

<HTML>

<HEAD>

<TITLE>APPLET EXAMPLE</TITLE>

</HEAD>

<BODY>

<APPLET CODE="First.java" width="200" height="200">

</APPLET>

</BODY>

</HTML>

GraphicsObject.drawLine() :
The drawLine() method is used in this program to draw the line. Here is
the syntax:
GraphicsObject.drawLine(int x_coordinate, int
y_coordinate, int x1_coordinate, int y1_coordinate);

Unit VII Programming in Java 5


Prepared by Manish ojha MCA

GraphicsObject.drawString() :
The drawSring() method is used in this program to draw string . Here is
the syntax :
GraphicsObject.drawString(String str, int x_coordinate, int y_coordinate);
GraphicsObject.drawOval() :
The drawOval() method is used to draws the circle. Here is the syntax :
GraphicsObject.drawOval(int x_coordinate, int y_coordinate, int width,
int height);
GraphicsObject.drawRect() :
The drawRect() method is used to draws the rectangle. Here is the
syntax :
GraphicsObject.drawRect(int x_coordinate, int y_coordinate, int Wdth,
int height);
import java.applet.*;
import java.awt.*;
public class Drawing extends Applet
{
public void paint(Graphics g)
{
// draws String
g.setColor(Color.blue); //
Now we tell g to change the color
g.setFont(new Font("Arial",Font.BOLD,14)); //
Now we tell g to change the font
g.drawString("Shape Drawing Applet", 100, 100);
// draws a Line
g.drawLine(90, 135, 90, 180);

Unit VII Programming in Java 6


Prepared by Manish ojha MCA

// draws a Oval
g.setColor(Color.black);
g.drawOval(0, 10, 100, 100);
// draws a Rectangle
g.setColor(Color.black);
g.drawRect(190, 50, 100, 100);
}
}
Differentiate between APPLET and Application:

APPLET Java Application


It doesn't contain main method. It contains main method.
It is a part of web page. It is standalone program.
It requires a java compatible browser. It can be run without browser.
Requires an HTML file. No HTML file.
Entry point is init( ) method. Entry point is main Method.
Generally used for GUI interface. Generally used for Console program.
Some probable questions:

1. What is stream? List its type.


2. What is input stream?
3. What is Output stream?
4. What is an applet? Embed a java applet program to a HTML file.
5. Write a Java program to read the student data from user and store it
in the file.

Unit VII Programming in Java 7

You might also like