Write byte to file using BufferedOutputStream

  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. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!