Menu



Java program for copying files


FileCopyUsingFOS.java

import java.io.*;
class FileCopyUsingFOS
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
// Declare the variables
FileInputStream fis = null;
FileOutputStream fos = null;
int fileSize = 0, data = 0;
// Initialize the variables
fis = new FileInputStream("source.txt");
// Check in that file data is available or not
fileSize = fis.available();
if (fileSize == 0)
{
System.out.println("Sorry, this file is empty.");
fis.close();
return;
}
// If file is not empty, initialse fos
fos = new FileOutputStream("dest.txt");
while ( (data = fis.read()) != -1)
{
fos.write(data);
}
// store the value from buffer to dest file
fos.flush();
System.out.println("File Copied.");
// close the connection
fos.close();
fis.close();
}
}