dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 11, 2002

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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);
				}
			}
		}
	}
}
Vivek Bagade
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date