Sie sind auf Seite 1von 4

1) Basic Streams in JAVA

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");

for (int i = 0; i <somedata.length;I++){


myFile.write(data[I]);
}
} catch (IOException e)
System.out.println("Could not write to a file: " + e.toString()); }
finally
// Close the file the same way as in example above }
b) Character Streams :- These provide a convenient way for handling character inputs and output. These
can be used for internationalization purpose as Character Streams uses UNICODE characters.
These streams end up calling by a word Reader or Writer like InputStreamReader, FileReader,
OutputStreamWriter, FileWriter etc.
Java uses two-byte characters to represent text data, and the classes FileReader and FileWriter work with
text files. These classes allow you to read files either one character at a time with read(), or one line at a
time with readLine().
The classes FileReader and FileWriter also support have buffering with the help of BufferedReader and
BufferedWriter. The following example reads text one line at a time:
FileReader myFile = null;
BufferedReader buff = null;
try {
myFile = new FileReader("abc.txt");
buff = new BufferedReader(myFile);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);
}
....
}
For the text output, there are several overloaded methods write() that allow you to write one character, one
String or an array of characters at a time.
To append data to an existing file while writing, use the 2-arguments constructor (the second argument
toggles the append mode):
FileWriter fOut = new FileWriter("xyz.txt", true);

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

To create a JAR file

jar cf jar-file input-file(s)

To view the contents of a JAR file

jar tf jar-file

To extract the contents of a JAR file

jar xf jar-file

To extract specific files from a JAR file

jar xf jar-file archived-file(s)

To run an application packaged as a JAR file (requires the Mainclass manifest header)

java -jar app.jar

To invoke an applet packaged as a JAR file

<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;

1).Path is an environment variable which import


is used
by the operating system to find the executables.
java.awt.Color;
import java.awt.Graphics;

public class DrawArcExample extends Applet{


Classpath is an environment variable which is
used by the Java compiler to find the path, of classes.ie in
public void paint(Graphics g){
J2EE we give the path of jar files.
//set color to red
setForeground(Color.red);

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)
*/

3).Path refers to the system while classpath refers to//this


thewill
Developing
Envornment.
draw an arc of width
50 & height 100 at (10,10)
g.drawArc(10,10,50,100,10,45);
/*
* To draw a filled arc use
* fillArc(int x1,int y1, int width, int height,int startAngle, int arcAngle)
* method of Graphics class.
*/
//draw filled arc
g.fillArc(100,10,100,100,0,90);

4) Small applet programs


}
}

/*
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);
}
}

Das könnte Ihnen auch gefallen