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);				}			}		}	}}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: