Menu
Topics Index
...
`

FilterOutputStreamDemo - Java Example Program


FilterOutputStreamDemo
import java.io.*;

class FilterOutputStreamDemo
{
    public static void main(String arg[])
    {
        File f = new File("E:/JavaPrograms/FolderOne/fileOne.java"); // LINE A
        if(f.exists())
            System.out.println("File exists.");
        else
            System.out.println("File not found.");
        FilterOutputStream fos = new FilterOutputStream(new FileOutputStream(f)); // LINE B
                                                                    
        
        String s = "I love programming.";
        byte[] b = s.getBytes(); // LINE C
        fos.write(b); // LINE D
        fos.close(); // LINE E
    
    }
}
OUTPUT

File exists.
I love programming.

DESCRIPTION

At LINE A we are declaring the path of the file. At LINE B we are creating the FilterOutputStream for FileOutputStream object so that to improve the performance. At LINE C we are converting the string to byte array. At LINE D we are writing into File At LINE E we are closing the Stream object.

THINGS TO TRY
  • Now write I LOVE JAVA to file.
  • Comment the LINE E and run the program. See the file to check the data. No data will be present since the Stream is not closed the file no data will be shown.

© meritcampus 2019

All Rights Reserved.

Open In App