Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsLink TO UsSEO Tools


HOME

J2ee tutorials

Java introduction
Java basic
Java installation
Java Packages
Learn Applets
Java Threads
Java Gui component
pixels


Java Event Handaling
Java Streams and Files
pixels
Java Swings
Java JDBC
pixels
Java Network Programming
Java RMI
pixels
Java Servlets
Java javabeans
pixels
EJB
Struts
pixels


Input stream and output stream







The input and output stream from the base class for classes like fileinputstream and byteinputstream. The input stream and output stream are abstract classes. So you cannot instantiate them directly. All the methods of inputstream and output stream throw IOException. Java provides the java.io package, which consist of platform independent classes such as files Random Access File, Input Stream and OutputStream to manipulate files and streams. The java.io package provides input stream and direct data form the user through the keyboard or file for instance, into the computer, and output streams that direct data towards output devices such as computer screen or file.



The inputstream class

The inputstream class represents the basic input stream. It defines methods that all input streams need.

The outputstream class

The outputstream provides the basic functionality for all output stream, some of te methods supported by outputstream are as follows.
Void write()--------------------------write data to the disk
Void flush()---------------------------forces any buffered output to be written
Void close()----------------------------closes the stream

Sample example for Reading characters form the screen

Import java.io.*;
Public class FirstProg
{
public static void main(String s[]) throws IOException
{
int n;
char c;
for (int i=0;i<5;i++);
{
System.out.println(“enter a character”);
n=System.in.read();
c=(char)n;
System.in.skip(2);
If(character.isLowerCase( c ))
{
System.out.println(“YOU ENETRED A LOWER CASE CHARACATER”+c);
}
else if(Character.isUpperCase( c ))
{
System.out.println(“you entered an upper case character” +c);
}
else
{
System.out.println(“you are not entering characters”);
}
}
}
}
FileInputStream and FileOutputStream Java has two classes for processing byte stream that are connected to files.
FileInputStream and FileOutputStream. These are the subclasses of InpoutStream and OutputStream.
Let us see the example


import java.io.*;
class FileInputStreamDemo
{
 public static void main(String args[]) throws IOException
{
FileOutputStream fos=new FileOutputStream("file1.txt");
byte inp[]=new byte[10];
System.out.println("Enter some data:" );

for(int i=0;i<10;i++)
{
  inp[i]=(byte)System.in.read();
}
fos.write(inp);
fos.close();
        FileInputStream fis=new  FileInputStream("file1.txt");
int size=fis.available();
for(int i=0;i

let us write the example of fileoutputstream

import java.io.*;
class FileStreamDemo
{
 public static void main(String args[]) throws Exception
{

FileOutputStream f1=new FileOutputStream("c:\file4.txt");
byte inp[]=new byte[10];
System.out.println("Enter some characters : ");
for(int i=0;i<10;i++)
{
inp[i]=(byte)System.in.read();
}
f1.write(inp);
f1.close();
     FileInputStream f=new FileInputStream("c:\file4.txt");
int size=f.available();
 for(int i=0;i

The BufferedInputStream BufferedOutputStream

The buffered streams are direct descendents of the filter stream. They read in more information than is immediately needed into a buffer. This increases efficiency as when a read occurs it is more likely to be from memory (fast) than from disk (slow). This buffering means they are particularly useful if you are reading in large amounts of data. The BufferedInputStream and BufferedOutputStream take an instance of a stream class as a constructor but may take size parameters. So you can tune the size of the buffer used

Sample example



import java.io.*;
class Bufferdemo1
{
   public static void main(String args[]) throws IOException
{
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("file1.txt"));
for(int i=1;i<10;i++)
{
bos.write((byte)i*11);
}
bos.close();
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("file1.txt"));
for(int i=1;i<10;i++)
{
System.out.println(bis.read());
}
}
}

DataInputStream and DataOutputStream

The DataInputStream and DataOutputStream are used to read binary representative of java primitives in a portable way. it gives you access to arrange of methods such as readDouble, readInt that will work the same on different platforms.

The sample example
//program to illustrate the DataInputStream and  DOStream
import java.io.*;
public class DataStream1
{
  public static void main(String args[])throws IOException
{ 	
	File f=new File("file1.txt");
                     DataOutputStream dout=new DataOutputStream(new FileOutputStream(f));
 for(int i=1;i<10;i++)
{
dout.writeDouble(i*3.142);
}
dout.close();
DataInputStream din=new DataInputStream(new FileInputStream(f));
for(int  i=1;i<10;i++)
{
 double d=din.readDouble();
System.out.println(d);
}
din.close();
}
}



Inform Friend About This Site

Your Name:

Friend's Name:

Friend's Email: