Skip to Content

Copy binary file using Streams

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