Java FilterOutputStream write() Example
FilterOutputStream class write() method example. This example shows you how to use write() method.
Syntax is : public void write(byte[] b) throws IOException
Method writes b.length bytes to this output stream. The write method of FilterOutputStream calls its write method of three arguments with
the arguments b, 0, and b.length.
Here is the code.
/**
* @(#) WriteFilterOutputStream.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 WriteFilterOutputStream extends FilterOutputStream {
WriteFilterOutputStream(FileOutputStream objFin) {
super(objFin);
}
public static void main(String[] args) throws IOException {
byte[] byteArray1 = new byte[]{'M', 'A', 'H', 'E', 'N', 'D', 'R', 'A'};
//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);
WriteFilterOutputStream obj = new WriteFilterOutputStream(objFStream);
// write method call.
obj.write(byteArray1);
// 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((char) c);
}
}
} |
output of the program.
| Content of the file : MAHENDRA |
|