![]() |
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; |
Easy peasy.
In the main method the args represents what was passed to the main method. This is what is invoked with a command line call. In Java, you can get these under the args[0] and args[1] in your code. Quotations are removed so if you have a path with a space in it, you can safely wrap it with quotations during the command line call, and it will be without in the String[] provided to main. |
Quote:
public main void (String[] args) { Just 3 lines? } |
Quote:
|
The main method signature in Java is only a string array.
To prevent out of bound exceptions, you can either try/catch them or simply verify the length first: PHP Code:
|
Quote:
Do i do something like this: Code:
FileCopy copier = new FileCopy(srcFileName, dstFileName); |
Nope, filecopy has to be constructed in the else after you extract the variables you need.
|
Quote:
|
Out of curiosity, how does one add exceptions to this? Would they be added to the copyFile method as additional catch phrases, i.e.
Code:
catch (ExceptionNameHere exec){ |
I see that doesn't work...Guess I need to figure that one out, and quick.
|
I don't understand the question.
Exception catching can be done anywhere which an exception can be thrown. |
I think I figured it out per our instructions. I'll respond again if I need help... thanks for checking in!
|
I can get the file to copy, however, when testing specific conditions as outlined in the assignment, I'm not getting errors as I should. There is a method that outlines the handling of each specific instance, which I have below:
Code:
private void commandParser(String[] args){• java FileCopy sourceFileName.txt too destinationFileName.txt • java FileCopy sourceFileName.txt to destinationFileName • java FileCopy sourceFileName to destinationFileName.txt • java FileCopy sourceFileName.txt to sourceFileName.doc • java FileCopy source.txt to destinationFileName.txt • java FileCopy sourceFileName.txt to destinationFileName.txt obviously, when I type in the last, it copies the file. However, the others do not elicit any error messages as intended. |
Apparently, I didn't pass the commandParser() method to args in the main. How do I do that??? (Sorry, I've been sitting at the PC for what feels like 3 days straight and I'm starting to burn out).:(
|
Doing this is completely pointless:
Code:
try{Exceptions should be used for when you do not know if if a condition may be raised, but could possibly be raised. You can throw your own exceptions, but you should be handling them in the caller if this is the case. Exceptions are raised by a block of code since they don't know what else to do with them. They leave it up to whatever called the method to determine how to proceed. IMO every one of these conditions above are valid. The only possible exception is sourceFileName.txt to sourceFileName.doc, and that is still debatable. The extension itself means nothing on the data within the file, so I don't see an issue with that. I'm not sure why you have a condition like this: if(srcFileName != "sourceFileName.txt" || dstFileName != "destinationFileName.txt"). Don't forget the rules of strings as well; use .equals for comparisons of immutable types, not == and !=. But these rules overall say you must use suorceFileName.txt and destinationFileName.txt, so what is the point of having the arguments provide to the java command line? |
| All times are GMT +1. The time now is 05:20 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.