Search Java Programs

Friday, February 5, 2010

Java error stream

The Java error stream is used to print error that arises during the execution of a program. The execution of program display an output and error to the different stream that allows the user to move them to different pages thereby isolating them. The output stream is used for command output, that is used to print the output of a command to a user.

Understand with Example

In this Tutorial we want to describe you a code that help you to understand java error stream, For this we have a class name'errorstreamexample'.Inside the main method if we have the length of the argument is not equal to 1.The System.err.println  display an message as passed in the argument of it. The  most often used from class System are the standard output error and standard error stream, these are the variable of System class.
System.err.println-This is Similar to System.out.println that is used to print the result.



public class errorstreamexample {

    public static void main(String[] args) {
       if(args.length!=1){
           System.err.println("usage: java.run.pathname");
           return;
       }
    }

}

Output

Write to a file

As we have read about  the BufferedOutputStream class that store the data in an internal buffer and lets you write characters to a stream. Lets see an example that writes the text "Hello Java" to the existed file.
The given example uses the BufferedOutputstream class that writes the bytes to the output stream. The output stream is a file "Filterfile.txt" in which the data is written in form of a byte. The getBytes( ) method of the String class returns the bytes from the string.

import java.io.*; 

class WriteFilter
{
    public static void main(String args[])
    {
    String str="Hello Java";
        try 
        {
      FileOutputStream fos = new FileOutputStream("Filterfile.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
      
            // Now write to the buffered stream.
           bos.write(str.getBytes());
           bos.flush();
                System.out.print("the data has been written");                 
        } 
        catch (Exception e) 
        {
            System.err.println("Error writing to a file: " + e);
        }
    }
}

Output of the Program:
C:\nisha>javac WriteFilter.java

C:\nisha>java WriteFilter
the data has been written
C:\nisha>

Read the File

As we have read about  the BufferedInputStream class that lets you read characters from a stream and stores it in an internal buffer. Lets see an example that reads the contains of the existed file.
The given example uses the BufferedInputstream class that reads the bytes from the input stream. The input stream is a file "Filterfile.txt" from which the data is read in form of a byte through the read( ) method. The read( ) method reads the byte from the file that is converted into the character-form.

import java.io.*; 

class ReadFilter
{
    public static void main(String args[])
    {
        try 
        {
      FileInputStream fin = new FileInputStream("Filterfile.txt");
            BufferedInputStream bis = new BufferedInputStream(fin);
      
            // Now read the buffered stream.
            while (bis.available() > 0) 
            {
                System.out.print((char)bis.read());
            }
        
        } 
        catch (Exception e) 
        {
            System.err.println("Error reading file: " + e);
        }
    }
}

Output of the Program:
C:\nisha>javac ReadFilter.java

C:\nisha>java ReadFilter
This is a text file
C:\nisha>

Website Design by Mayuri Multimedia