Skip to Content

StringBuffer To File Java Example

  • 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.         StringBuffer To File Java Example
  3.         This example shows how to write contents of StringBuffer to file using BufferedWriter
  4.         and FileWriter Java classes.
  5. */
  6.  
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11.  
  12. public class JavaStringBufferToFileExample {
  13.        
  14.         public static void main(String[] args) throws IOException {
  15.                
  16.                 //create StringBuffer object
  17.                 StringBuffer sbf = new StringBuffer();
  18.                
  19.                 //StringBuffer contents
  20.                 sbf.append("StringBuffer contents first line.");
  21.                 //new line
  22.                 sbf.append(System.getProperty("line.separator"));
  23.                 //second line
  24.                 sbf.append("StringBuffer contents second line.");
  25.                
  26.                 /*
  27.                  * To write contents of StringBuffer to a file, use
  28.                  * BufferedWriter class.
  29.                  */
  30.                
  31.                 BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("d:/demo.txt")));
  32.                
  33.                 //write contents of StringBuffer to a file
  34.                 bwr.write(sbf.toString());
  35.                
  36.                 //flush the stream
  37.                 bwr.flush();
  38.                
  39.                 //close the stream
  40.                 bwr.close();
  41.                
  42.                 System.out.println("Content of StringBuffer written to File.");
  43.         }
  44. }
  45.  
  46. /*
  47. Output of above given Write StringBuffer to File example would be
  48. Content of StringBuffer written to File.
  49.  
  50. Contents of file "demo.txt" would be
  51. StringBuffer contents first line.
  52. StringBuffer contents second line.
  53. */