Skip to Content

Write byte to file using BufferedOutputStream

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.         Write byte to file using BufferedOutputStream
  3.         This example shows to write a byte to file using Java
  4.         BufferedOutputStream. BufferedOutputStream buffers the output so
  5.         that the appication does not have to call underlying output stream
  6.         each time the byte is written.
  7. */
  8.  
  9. import java.io.BufferedOutputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. public class WriteByteToFile {
  15.  
  16.         public static void main(String[] args) {
  17.                
  18.                 String strFile = "C:/FileIO/BufferedOutputStreamDemo.txt";
  19.                 BufferedOutputStream bos = null;
  20.                
  21.                 try
  22.                 {
  23.                         //Create FileOutputStream for the file
  24.                         FileOutputStream fos = new FileOutputStream(strFile);
  25.                        
  26.                         //Create BufferedOutputStream object for FileOutputStream
  27.                         bos = new BufferedOutputStream(fos);
  28.                        
  29.                         byte b = 66;
  30.                        
  31.                         /*
  32.                          * To write a byte to BufferedOutputStream use,
  33.                          * public void write(int b) method of BufferedOutputStream
  34.                          * class.
  35.                          *
  36.                          * This method may throw IOException if something goes
  37.                          * wrong.
  38.                          */
  39.                          
  40.                          System.out.println("BufferedOutputStream: writing file");
  41.                          
  42.                          //this will write a character represented by ascii 66
  43.                          //i.e. 'B'
  44.                          bos.write(b);
  45.                          
  46.                         System.out.println("BufferedOutputStream : written file");
  47.                 }
  48.                 catch(FileNotFoundException fnfe)
  49.                 {
  50.                         System.out.println("Specified file not found" + fnfe);
  51.                 }
  52.                 catch(IOException ioe)
  53.                 {
  54.                         System.out.println("Error while writing to file" + ioe);
  55.                 }
  56.                 finally
  57.                 {
  58.                         try
  59.                         {
  60.                                 if(bos != null)
  61.                                 {
  62.                                         bos.flush();
  63.                                         bos.close();
  64.                                 }
  65.                                                                
  66.                         }
  67.                         catch(Exception e){}
  68.                        
  69.                 }
  70.                
  71.         }
  72. }
  73.  
  74. /*
  75. Output would be
  76. BufferedOutputStream: writing file
  77. BufferedOutputStream: written file
  78.  
  79. File content would be
  80. B
  81. */