Write byte array to file using BufferedOutputStream

  1. /*
  2. Write byte array to file using BufferedOutputStream
  3. This example shows how to write a byte array to file using
  4. java BufferedOutputStream.
  5. */
  6.  
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.FileOutputStream;
  12.  
  13. public class WriteByteArrayToFile {
  14.  
  15. public static void main(String[] args) {
  16.  
  17. String strFileName = "C:/FileIO/BufferedOutputStreamDemo";
  18. BufferedOutputStream bos = null;
  19.  
  20. try
  21. {
  22. //create an object of FileOutputStream
  23. FileOutputStream fos = new FileOutputStream(new File(strFileName));
  24.  
  25. //create an object of BufferedOutputStream
  26. bos = new BufferedOutputStream(fos);
  27.  
  28. String str = "BufferedOutputStream Example";
  29.  
  30. /*
  31. * To write byte array to file use,
  32. * public void write(byte[] b) method of BufferedOutputStream
  33. * class.
  34. */
  35. System.out.println("Writing byte array to file");
  36.  
  37. bos.write(str.getBytes());
  38.  
  39. System.out.println("File written");
  40. }
  41. catch(FileNotFoundException fnfe)
  42. {
  43. System.out.println("Specified file not found" + fnfe);
  44. }
  45. catch(IOException ioe)
  46. {
  47. System.out.println("Error while writing file" + ioe);
  48. }
  49. finally
  50. {
  51. if(bos != null)
  52. {
  53. try
  54. {
  55.  
  56. //flush the BufferedOutputStream
  57. bos.flush();
  58.  
  59. //close the BufferedOutputStream
  60. bos.close();
  61.  
  62. }
  63. catch(Exception e){}
  64. }
  65. }
  66.  
  67. }
  68. }
  69.  
  70. /*
  71. Output would be
  72. Writing byte array to file
  73. File written
  74.  
  75. File contents would be
  76. BufferedOutputStream Example
  77. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




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!