Skip to Content

Write String as bytes to a file using DataOutputStream

  • 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 String as bytes to a file using DataOutputStream
  3.   This Java example shows how to write a Java String value to a file
  4.   as a sequence of bytes using writeBytes method of Java
  5.   DataOutputStream class.
  6. */
  7.  
  8. import java.io.DataOutputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. public class WriteStringAsBytesToFile {
  13.  
  14.   public static void main(String[] args) {
  15.    
  16.     String strFilePath = "C://FileIO//WriteStringAsBytes.txt";
  17.    
  18.     try
  19.     {
  20.       //create FileOutputStream object
  21.       FileOutputStream fos = new FileOutputStream(strFilePath);
  22.      
  23.       /*
  24.        * To create DataOutputStream object from FileOutputStream use,
  25.        * DataOutputStream(OutputStream os) constructor.
  26.        *
  27.        */
  28.      
  29.        DataOutputStream dos = new DataOutputStream(fos);
  30.      
  31.        String str = "This string will be written to file as sequence of bytes!";
  32.      
  33.        /*
  34.         * To write a string as a sequence of bytes to a file, use
  35.         * void writeBytes(String str) method of Java DataOutputStream class.
  36.         *
  37.         * This method writes string as a sequence of bytes to underlying output
  38.         * stream (Each character's high eight bits are discarded first).
  39.         */
  40.        
  41.         dos.writeBytes(str);
  42.        
  43.         /*
  44.          * To close DataOutputStream use,
  45.          * void close() method.
  46.          *
  47.          */
  48.        
  49.          dos.close();
  50.        
  51.     }
  52.     catch (IOException e)
  53.     {
  54.       System.out.println("IOException : " + e);
  55.     }
  56.  
  57.   }
  58. }