Java Reference
In-Depth Information
System.exit(0);
}
// Code for createBackupFile() goes here...
}
You could try this out by copying the source for the program using the command:
java FileCopy FileCopy.java
You should get output something like:
File copy complete. 3036 bytes copied to D:\Beg Java
1.4\Examples\FileCopy _ backup.java
Of course, if the source file layout is different or you have a few more - or less - comments, the number
of bytes copied will be different. Also the file path will be your path, not mine. In any event, you should
be able to check that the new file's contents are identical to the original.
How It Works
We first obtain the command line argument and create a File object from it with the code:
if(args.length==0) {
System.out.println("No file to copy. Application usage is:\n"+
"java -classpath . FileCopy \"filepath\"" );
System.exit(1);
}
File fromFile = new File(args[0]);
If there's no command line argument we supply a message explaining how to use the program.
Next we verify that this is a real file:
if(!fromFile.exists()) {
System.out.println("File to copy, "+fromFile.getAbsolutePath()
+ ", does not exist.");
System.exit(1);
}
If it isn't, there's nothing we can do, so we bail out of the program.
Creating a File object for the backup file is a piece of cake:
File toFile = createBackupFile(fromFile);
We saw how this method works earlier.
Search WWH ::




Custom Search