Unit VII IOand Java Applet
Unit VII IOand Java Applet
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
try
String name;
int rollno;
name = sc.next();
rollno = sc.nextInt();
String r = String.valueOf(rollno);
fw.write(name+" ");
fw.write(r);
fw.close();
catch(Exception e)
System.out.println(e.getMessage());
Features:
in the applet life cycle are init() and destroy(). Other methods execute
multiple times.
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("WELCOME",150,150);
} }
First.html
<HTML>
<HEAD>
<TITLE>APPLET EXAMPLE</TITLE>
</HEAD>
<BODY>
</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);
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);
// 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: