by Dinesh Thakur Category: Stream

There are numerous classes available for reading/writing bytes from/to file, memory or even a network socket. We have already given a brief overview of byte stream classes and we shall now discuss them in detail.

The FilelnputSteam and the FileOutputStream classes are used to read and write bytes sequentially from a file. The FilelnputStream class extends InputStream class and therefore inherits all its methods. However, it does not define any new methods of its own. Similarly, the FileOutputStream class inherits all the methods of the OutputStream class which it extends and does not define any new methods of its own.

In order to open a file for input, create a FilelnputStream object. For this, the FilelnputStream class provides the following constructors.

FilelnputStream(String fileName)
FilelnputStream(File fileObj)
Here, fileName specifies the name ofthe file and fileObj is a File object that describes the file. If the file to be opened doesnot exist, both these constructors throw java.io.FileNotFoundException.

In order to open a file for output, create a FileOutputStream object. For this, the
FileOutputStream class provides the following constructors.
FileOutputStream(File fileObj)
FileOutputStream(String fileName)
FileOutputStream(File fileObj, boolean append)
FileOutputStream(String fileName, boolean append)
Here, fileName specifies the name ofthe file and fileObj is a File object that describes the file. If the file dose not exist, new file would be created. If the file exists, the first two constructors overwrite the existing contents of the file. To retain the existing contents and append the new data at the end of the file, use the last two constructors and let the second argument append to true. A java.io.FileNotFoundException is thrown if
• File exists but is a directory rather thana regular file.
• File doesnot exist but cannot be created.
• File cannot be opened for any other reason.

Now let us consider a program that demonstrates how to write bytes to the file CopyDataFiletoFile.dat using FileOutputStream and read-them back from the same file using the FilelnputStream.

Here is the Java Example for Copy Data From One File to Another:

import java.io.*;

class CopyDataFiletoFile
{
             public static void main(String args[])throws IOException
        {
              FileInputStream Fread =new FileInputStream("Hello.txt");
              FileOutputStream Fwrite=new FileOutputStream("Hello1.txt") ;
              System.out.println("File is Copied");
              int c;
              while((c=Fread.read())!=-1)
              Fwrite.write((char)c);
              Fread.close();
              Fwrite.close();
         }
}

 

Copy Data From One File to Another File



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles