Java FilterOutputStream write() Example

FilterOutputStream class write() method example. This example shows you how to use write() method.


Syntax is : public void write(int b) throws IOException
This method writes the specified byte to this output stream. The write method of FilterOutputStream calls the write method of its
underlying output stream, that is, it performs out.write(b).

Here is the code.

/**
 * @(#) Write2FilterOutputStream.java
 * A class representing use of method write() of FilterOutputStream 
class in java.io Package.
 * @Version  10-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class Write2FilterOutputStream extends FilterOutputStream {

    Write2FilterOutputStream(FileOutputStream objFin) {
        super(objFin);
    }

    public static void main(String[] argsthrows IOException {

        //Create a file with a specified name and file extension.
        File file = new File("mahendra.txt");

        // Create a output stream and write specified bytes in file.
        FileOutputStream objFStream = new FileOutputStream(file);
        Write2FilterOutputStream obj = new Write2FilterOutputStream(objFStream);

        // write method call.
        obj.write(77);   // ascii code of alphabet 'M'
        obj.write(65);   // ascii code of alphabet 'A'
        obj.write(72);   // ascii code of alphabet 'H'
        obj.write(69);   // ascii code of alphabet 'E'
        obj.write(78);   // ascii code of alphabet 'N'
        obj.write(68);   // ascii code of alphabet 'D'
        obj.write(82);   // ascii code of alphabet 'R'
        obj.write(65);   // ascii code of alphabet 'A'

        // create a file input stream to read content of the file.
        InputStream in = new FileInputStream(file);
        System.out.print("Content of the file : ");
        for (int c = in.read(); c != -1; c = in.read()) {
            System.out.print((charc);
        }
    }
}

output of the program.
 Content of the file : MAHENDRA

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US