Beruflich Dokumente
Kultur Dokumente
Java defines two types of streams. They area) Byte Streams :- These are the most basic type of Stream for input output operations and are used to
process data byte by byte. Byte based streams generally end up calling by word Stream likeInputStream
and
OutputStream and
are
used
to
process
a
raw
Byte
at
a
time.
Some Exceptions are DataInputStream and DataOutputStream which are used to read and write Java
standard datatypes like int, long, float and double.
If a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or
OutputStream respectively. The example below shows how to use the class FileInputStream to read a file
named abc.dat. This code snippet prints each byte's value separated with white spaces. Byte values are
represented by integers from 0 to 255, and if the read() method returns -1, this indicates the end of the
stream.
import java.io.FileInputStream;
import java.io.IOException;
public class ReadingBytes {
public static void main(String[] args) { FileInputStream myFile = null; try {
myFile = new FileInputStream("c:\\abc.dat"); // open the stream
boolean eof = false;
while (!eof) {
int byteValue = myFile.read(); // read the stream
System.out.println(byteValue);
if (byteValue == -1){
eof = true;
}
//myFile.close(); // do not do it here!!!
}
}catch (IOException e) {
System.out.println("Could not read file: " + e.toString());
} finally{
try{
if (myFile!=null){
myFile.close(); // close the stream
}
} catch (Exception e1){
e1.printStackTrace();
}
}
}
}
The next code fragment writes into the file xyz.dat using the class FileOutputStream:
int somedata[]={56,230,123,43,11,37};
FileOutputStream myFile = null;
try {
myFile = new FileOutputStream("c:\xyz.dat");
2) JAR
A JAR (Java ARchive) file is a file that contains the class, image, and sound files for a Java application
or applet gathered into a single file and possibly compressed. When a programmer gets a Java program
development kit, a small program or utility called "jar" is included. The jar utility lets the programmer
create, list, or extract the individual files from a JAR file. In an enterprise, a Java application can be
started with a set of JAR files for use during execution. An off-the-shelf open source package can be
delivered as a JAR file and run with XML data.
Common JAR file operations
Operation
Command
jar tf jar-file
jar xf jar-file
To run an application packaged as a JAR file (requires the Mainclass manifest header)
<applet
code=AppletClassName.class
archive="JarFileName.jar"
width=width height=height>
</applet>
Draw Arc in Applet Window Example
This java example shows how to draw arc in an applet window using
3) Difference between PATH and CLASSPATH
in JAVA.
drawArc method of Graphics class. It also shows how to draw a filled
arcs using fillArc method of Graphics class.
PATH and CLASSPATH are operating system level environment variables. PATH is used to define where
<applet
code="DrawOvalsExample"
width=500
height=500>
the system can find the executables (.exe) files
whereas
CLASSPATH
is used
to specify the location of
</applet>
.class files.
import java.applet.Applet;
2).PATH is nothing but setting up an environment /*for operating system. Operating System will look in
* to draw an arc in an applet window use,
this PATH for executables.
* void drawArc(int x1,int y1, int width, int height,int startAngle, int arcAngle)
* method.
*
* This
method
drawsJava
an arc ofwill
specified
width
Classpath is nothing but setting up the environment
for
Java.
use
toandfind compiled classes
* height at (x1,y1)
*/
/*
Draw Oval & Circle in Applet Window Example
This java example shows how to draw ovals & circles in an applet window using
drawOval method of Graphics class. It also shows how to draw a filled
ovals and circles using fillOval method of Graphics class.
*/
/*
<applet code="DrawOvalsExample" width=500 height=500>
</applet>
*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class DrawOvalsExample extends Applet{
public void paint(Graphics g){
//set color to red
setForeground(Color.red);
/*
* to draw a oval in an applet window use,
* void drawOval(int x1,int y1, int width, int height)
* method.
*
* This method draws a oval of specified width and
* height at (x1,y1)
*/
//this will draw a oval of width 50 & height 100 at (10,10)
g.drawOval(10,10,50,100);
/*
* To draw a filled oval use
* fillOval(int x1,int y1, int width, int height)
* method of Graphics class.
*/
//draw filled oval
g.fillOval(100,20,50,100);
}
}