0% found this document useful (0 votes)
8 views2 pages

File

This Java program demonstrates how to copy the contents of a file named 'source.txt' to another file named 'destination.txt' using FileInputStream and FileOutputStream. It reads the source file byte by byte and writes each byte to the destination file, handling potential IOExceptions. Finally, it ensures that both input and output streams are closed properly in a finally block.

Uploaded by

Computer Network
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

File

This Java program demonstrates how to copy the contents of a file named 'source.txt' to another file named 'destination.txt' using FileInputStream and FileOutputStream. It reads the source file byte by byte and writes each byte to the destination file, handling potential IOExceptions. Finally, it ensures that both input and output streams are closed properly in a finally block.

Uploaded by

Computer Network
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import java.io.

FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileCopyExample


{

public static void main(String[] args)


{

FileInputStream inputStream = null;

FileOutputStream outputStream = null;

try
{

// Open the input file stream

inputStream = new FileInputStream("source.txt");

// Open the output file stream

outputStream = new FileOutputStream("destination.txt");

int byteData;

// Read the file byte by byte and write to another file

while ((byteData = inputStream.read()) != -1)


{

outputStream.write(byteData);

System.out.println("File copied successfully!");

}
catch (IOException e)
{
e.printStackTrace();

}
finally
{

try
{

if (inputStream != null)
{

inputStream.close();

if (outputStream != null)
{

outputStream.close();

}
catch (IOException e)
{

e.printStackTrace();

You might also like