devxlogo

Copy Data from One File to Another

Copy Data from One File to Another

This wrapper class “FileCopier” copies data from one file to another. This class uses another class: “StreamCopier”.

  /** * Class: FileCopier * Author: Vivek Bagade * Date: 8/31/2001 * Description: Copies from one file to another file */import java.io.*;/** */public class FileCopier{	public static void main(String[] args)	{	}	public static void copy(String inFile, String outFile)		throws IOException {		FileInputStream fin = null;		FileOutputStream fout = null;		try {			fin = new FileInputStream(inFile);			fout = new FileOutputStream(outFile);			StreamCopier.copy(fin, fout);		}		finally {			try {				if(fin != null) fin.close();			}			catch(IOException e) {			}			try {				if(fin != null) fin.close();			}			catch(IOException e) {			}		}	}	public static void copy(File inFile, File outFile)		throws IOException {		copy(inFile.getAbsolutePath(), outFile.getAbsolutePath());	}	public static int createFileSafely(File f)	{		try {			if(f.exists()) {				return 0;			}			FileOutputStream fout = new FileOutputStream(f);			fout.close();			return 1;		}		catch(Exception e)		{			e.printStackTrace();			return 2;		}	}}/** * Class: StreamCopier * Author: Vivek Bagade * Date: 8/31/2001 * Description: Copies from input stream to outputstream */import java.io.*;/** */public class StreamCopier{	public static void main(String[] args)	{	}	public static void copy(InputStream in, OutputStream out)		throws IOException {		synchronized(in) {			synchronized(out) {				byte[] buffer = new byte[256];				while(true) {					int bytesRead = in.read(buffer);					if(bytesRead == -1) break;					out.write(buffer,0,bytesRead);				}			}		}	}}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist