Flush output stream

/*

  Flush output stream

  This Java example shows how to flush output stream using flush method of

  DataOutputStream class.

*/

 

import

java

.

io

.

DataOutputStream

;

import

java

.

io

.

FileOutputStream

;

import

java

.

io

.

IOException

;

 

 

public

class

FlushStream

{

 

  

public

static

void

main

(

String

[

]

args

)

{

    

String

strFilePath

=

“C://FileIO//WriteByte.txt”

;

  

    

try

    

{

      

//create FileOutputStream object

      

FileOutputStream

fos

=

new

FileOutputStream

(

strFilePath

)

;

    

      

/*

       * To create DataOutputStream object from FileOutputStream use,

       * DataOutputStream(OutputStream os) constructor.

       *

       */

      

      

DataOutputStream

dos

=

new

DataOutputStream

(

fos

)

;

      

      

String

strContent

=

“This example shows how to flush output stream!”

;

      

dos

.

writeBytes

(

strContent

)

;

      

      

/*

        * To flush output stream, use

        * void flush() method of DataOutputStream class.

        *

        * This method internally calls flush method of underlying OutputStream

        * class which forces any buffered output bytes to be written in the stream.

        */

      

        

dos

.

flush

(

)

;

      

        

//close the stream

        

dos

.

close

(

)

;

        

    

}

    

catch

(

IOException

e

)

    

{

      

System

.

out

.

println

(

“IOException : “

+

e

)

;

    

}

 

  

}

}