Home   Family   Softwares   Resume   Activities  

// This program will write from one file into another file.
// The command to be given on command prompt will be as follows
// java iotest file1.txt  file2.txt
// file1.txt is the file from where the data is to be read
// file2.txt is file into which data is to be written

import java.io.*;

public class IOTest
       { 
             public static void main(string[] args)
                          //To display error message if two arguments in the form of two file names is not passed
                        if (args.length != 2)
                    {
                        System.out.println ("Syntax : Write names of two files"); 
                         //Exit the program  
                        System.exit(0); }
                    { 
                      try
                          {
                          // open arg[0] for input
                             FileInputStream in = new FileInputStream(args[0]); 
                         // add buffering to that InputStream
                             BufferedInputStream bin = new BufferedInputStream(in);
                        // open arg[1] for output and add buffering to it as well
                             FileOutputStream out = new FileOutputStream(args[1]);
                             BufferedOutputStream bout = new BufferedOutputStream(out);


                       //now read as long=as there is something to read
                             byte bArray[] = new byte[256]; 
                             int nBytesRead;

                             while(bin.available( ) > 0 )
                              {

                                       //read up a block - remenber how many bytes read
                                         nBytesRead = bin.read(bArray);  

                                      // write that many bytes back out starting
                                     // at offset 0 in the array
                                        
bout.write(bArray, 0, nBytesRead);
                              }
                                        bout.close();
                       }
                       catch(IOException ioe)
                              {
                                        System.out.print(ioe.tostring( ) );
                              }
                       }
      }


 

Hosted by www.Geocities.ws

1