Created
February 28, 2012 17:15
-
-
Save RobertShippey/1933765 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* use import to include classes defined within the java.io package | |
*/ | |
import java.io.*; | |
/** | |
* This example will demonstrate how to write information to a file. | |
* <p> | |
* It also shows you how to use comments that can be used by javadoc to produce | |
* HTML api document files for your class. There are a number of javadoc tags that | |
* can be used. These are prefixed by a "@". HTML code can also be inserted into | |
* javadoc comments as well. | |
* <p> | |
* This example requires the use of some new classes: | |
* <p><code>PrintWriter</code> | |
* <p><code>FileWriter</code> | |
* <p><code>IOException</code> | |
* | |
* <p>These are defined within the java.io package and more information can be found at: | |
* <p><a href="http://download.oracle.com/javase/6/docs/api"> Java online API</a> | |
* | |
* <p>These can be included in two ways: | |
* <p><code>import java.io.*;</code> will include all classes contained within the java.io package | |
* <p> or | |
* <p><code>import.java.PrintWriter;</code> | |
* <p><code>import java.io.FileWriter;</code> | |
* <p><code>import java.io.IOException;</code> | |
* | |
* <p>This allows you to selectively import only the classes you need instead of everything. | |
* | |
* @version 1 | |
*/ | |
public class Example3 { | |
/** | |
* The main method demonstrates how to:- | |
* - check for 3 arguments | |
* - creating an instance of the Example3 class | |
* - how to call a method | |
* - how to handle exceptions. | |
* @param args The command line arguments | |
*/ | |
public static void main(String[] args) { | |
// check to see if expected number of args passed in | |
if( args.length != 3 ) { | |
// incorrect parameters so display usage message and exit | |
System.err.println("Usage: java Example3 <file> <starting number> <ending number>"); | |
System.err.println(" : You must supply three argument values!"); | |
// terminate program | |
System.exit(0); | |
} else { | |
// correct number of parameters, so create instance of class | |
Example3 myTest = new Example3(); | |
// get parameters and store in local variables | |
String fileName = args[ 0 ]; | |
int startValue = Integer.parseInt( args[1] ); | |
int endValue = Integer.parseInt( args[2] ); | |
try { | |
/* | |
* File operations can generate IO errors. | |
* Therefore, we need to be able to capture an IO error if one is generated. | |
* The try {} catch( ... ) {} block provides us with a way to capture an error | |
* and process what to do if an error occurs. | |
*/ | |
// call local method | |
myTest.writeToFile( fileName, startValue, endValue ); | |
} catch( IOException error ) { | |
/* | |
* An IO Exception has occured | |
* Display a message which says why things went wrong. Exceptions provide us | |
* with a number of methods. The getMessage() method returns a string describing | |
* what the reason of the error is. | |
*/ | |
System.err.println( "Error! - " + error.getMessage() ); | |
/* | |
* We can also get the exception to display information about what class | |
* and method caused the problem. | |
*/ | |
error.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* This method demonstrates how to write information to a file. | |
* @param fileName String representation of file name | |
* @param start int containing the starting number | |
* @param end int containing the end number to count to | |
* @exception IOException | |
*/ | |
public void writeToFile( String fileName, int start, int end ) throws IOException { | |
/* File operations can generate IO errors. | |
* Therefore, we need to be able to capture an IO error if one is generated. | |
* The PrintWriter or FileWriter classes can return an error, so this method | |
* must return the exception back to the calling method. This is done by | |
* adding "throws" and the exception type to the method signature. | |
*/ | |
// create a filewriter object to let us output things to a file | |
FileWriter fileOutput = new FileWriter(fileName,false); | |
// create a printwriter so that we can print to the associated file | |
// using println | |
PrintWriter printOutput = new PrintWriter(fileOutput,true); | |
// loop example to send information to file | |
for(int i=start; i<end; i++) { | |
// write the value of i to the fi;e | |
printOutput.println( i ); | |
} | |
// after finishing, have to close file | |
fileOutput.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment