Copy binary file using Streams

  1. /*
  2. Copy binary file using Streams
  3. This example shows how to copy a binary file using Java FileInputStream
  4. and FileOutputStream classes. If you want to copy text file use
  5. FileReader and FileWriter classes instead of FileInputStream and
  6. FileOutputStream classes.
  7. */
  8.  
  9. import java.io.FileNotFoundException;
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14.  
  15. public class CopyBinaryFile {
  16.  
  17. public static void main(String[] args) {
  18.  
  19. String strSourceFile="C:/FileIO/source.dat";
  20. String strDestinationFile="C:/FileIO/dest.dat";
  21.  
  22. try
  23. {
  24. //create FileInputStream object for source file
  25. FileInputStream fin = new FileInputStream(strSourceFile);
  26.  
  27. //create FileOutputStream object for destination file
  28. FileOutputStream fout = new FileOutputStream(strDestinationFile);
  29.  
  30. byte[] b = new byte[1024];
  31. int noOfBytes = 0;
  32.  
  33. System.out.println("Copying file using streams");
  34.  
  35. //read bytes from source file and write to destination file
  36. while( (noOfBytes = fin.read(b)) != -1 )
  37. {
  38. fout.write(b, 0, noOfBytes);
  39. }
  40.  
  41. System.out.println("File copied!");
  42.  
  43. //close the streams
  44. fin.close();
  45. fout.close();
  46.  
  47. }
  48. catch(FileNotFoundException fnf)
  49. {
  50. System.out.println("Specified file not found :" + fnf);
  51. }
  52. catch(IOException ioe)
  53. {
  54. System.out.println("Error while copying file :" + ioe);
  55. }
  56. }
  57. }
  58.  
  59. /*
  60. Typical output would be
  61. Copying file using streams
  62. File copied!
  63. */

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!