From JavaWIDE
|
01 //start auto-imports
02 //end auto-imports
03 importimport means to make the classes and/or packages available in this program java.util.*;
04 importimport means to make the classes and/or packages available in this program java.io.*;
05 importimport means to make the classes and/or packages available in this program java.net.*;
06 /**
07 * A simple application to read text from a file on the wiki.
08 * @authorthis is the Javadoc tag for documenting who created the source code Jam Jenkins
09 */
10
11 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects SampleTextFileReader
12 {open braces start code blocks and must be matched with a close brace
13 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.( String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args )
14 {open braces start code blocks and must be matched with a close brace
15
16
17 trytry is for executing a code block that may experience exceptions (errors) //the following code may cause an exception if the file does not exist
18 {open braces start code blocks and must be matched with a close brace
19
20 //open the input stream using the file name
21 File file =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor File( "Sample101.txt" );
22 Scanner scanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner( file );
23 //read a line at a time
24 scanner.useDelimiter( "\n" );
25 //read until there are no lines left and
26 //print the results to the console
27
28 whilewhile is a looping structure for executing code repeatedly ( scanner.hasNext() )
29 System.out.println( scanner.next() );
30 }close braces end code blocks and must match an earlier open brace
31
32 catchcatch means to handle an exception that may occur ( IOException e ) //in case the file does not exist, say so
33 {open braces start code blocks and must be matched with a close brace
34 e.printStackTrace( System.err );
35 }close braces end code blocks and must match an earlier open brace
36 }close braces end code blocks and must match an earlier open brace
37 }close braces end code blocks and must match an earlier open brace
|
Download/View SampleTextFileReader.java