Thread: Copying a File
View Single Post
Old 10-19-2012, 03:50 PM   PM User | #1
ubiByte
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
ubiByte is an unknown quantity at this point
Copying a File

Hi guys it's my first time here, I hope you guys can help me out with my question.

I'm to write a program that copies a file, I've already done that it works at copying but what I need help with is the professor wants to be able to do this from the command line:

java FileCopy sourceFileName.txt to destinationFileName.txt

How do i get my program to respond to such command from command prompt?
This is my code it copies the file when it's run from Eclipse,

Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class FileCopy
{
	String srcFileName;
	String dstFileName;
	String line;
	BufferedReader source;
	PrintWriter destination;
	File fileSource;
	File destinationFile;
	
	public FileCopy(String src, String dest)
	{
		this.srcFileName = src;
		this.dstFileName = dest;
		fileSource = new File(this.srcFileName);
		destinationFile = new File(this.dstFileName);
		try 
		{
			this.copyFile();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	private boolean openFile()
	{
		try 
		{
			if (fileSource.exists())
			{
				source = new BufferedReader(new FileReader(fileSource));
				return true;
			}
		}
		catch (FileNotFoundException e) 
		{
			e.getMessage();
		}
		return false;
	}
	
	private boolean copyFile() throws IOException
	{
		if (this.openFile() == true)
		{
			try 
			{
				destination = new PrintWriter(destinationFile);

				do 
				{
					line = source.readLine();
					if (line != null) 
					{
						destination.write(line + "\n");
					}
				}
				
				while (line != null);
				source.close();
				destination.close();
				return true;
			} 
			catch (FileNotFoundException e) 
			{
				e.getMessage();
			}
		}
		return false;
	}
	
	public static void main (String[] args)
	{
		FileCopy newCopy = new FileCopy("sourceFile.txt", "destinationFile.txt");
	}

}
ubiByte is offline   Reply With Quote