![]() | ![]() | ![]() | 4.10 Week 10 -- I/O |
Buffered
- efficiency.
Cat2
/* Louis Taber PCC 4/21/2003 */ /* File I/O demo program */ import java.util.*; import java.io.*; class Cat { public static void main( String [] args) { boolean show_usage = false; BufferedReader Input = new BufferedReader( new InputStreamReader( System.in ) ); PrintStream Output = System.out; PrintStream Error = System.err; ARGS: for(int i=0; i<args.length; i++) { if( ! args[i].startsWith("-") ) continue; if( args[i].length() < 2 ) break; switch( args[i].charAt(1) ) { case 'i': i++; // move to next argument if( i >= args.length) { show_usage = true; break ARGS; } try { Input = new BufferedReader( new FileReader( args[i].trim() ) ); } catch( FileNotFoundException e ) { Error.println( "Input file not found:\n" + e ); } System.out.println( "-i " + args[i]); continue ARGS; case 'o': // Change output file i++; // move to next argument if( i >= args.length) { show_usage = true; break ARGS; } try { Output = new PrintStream( new FileOutputStream( args[i].trim() ) ); } catch( FileNotFoundException e ) { Error.println( "Output file not found:\n" + e ); } case 'l': // Change error output to log file System.out.println( "-l "); i++; // move to next argument if( i >= args.length) { show_usage = true; break ARGS; } try { Error = new PrintStream( new FileOutputStream( args[i].trim() ) ); } catch( FileNotFoundException e ) { Error.println( "Log file not found:\n" + e ); } break; case 'h': case 'H': System.out.println( "Help information: "); default: show_usage = true; continue ARGS; } System.out.println( args[i]); } if( show_usage ) { System.out.println( "Print usage "); System.exit(1); } Error.println( "An error "); String input_string; try { while( ( input_string = Input.readLine()) != null ) Output.println( input_string ); } catch( IOException e ) { System.err.println( "File close() error:\n" + e ); } try{ Input.close(); } catch( IOException e ) { System.err.println( "File close() error:\n" + e ); } Output.close(); Error.close(); System.exit(0); } }
compareTo()
method.
This program will also deal with:
substring
trim()
& parseInt()
Input a data file (or the standard input), sort the file by the
specified keys, and write out the sorted data. Handle files of "all"
lengths. From no records to over 1,000,000. By default, input the data
as a String
and do an ascending sort.
-i
" option, use the following argument
for the input file name. If there is no following argument,
print out an error message and usage information.
-o
" for the output file name.
Otherwise use the standard output.
-h
", print out the usage information.
compareTo()
method.
(compareTo()
is already written for String
.)
-f
" followed by a
sort field specifier. The fields are comma separated. No spaces are
allowed. Each field has a required start and stop column.
These are hyphen separated and inclusive. There may be an optional
"r
" to reverse the sort, and an optional "n
" to
treat the field numerically.
StringTokenizer(String str, String delim, boolean returnDelims)
to break up the sort field argument.
^
operator works on boolean arguments.
-F
" is present, the sort information is in the following
file name.
stderr
file unless a "-l
"
option is specified, the send it to the specified log file.
Log the line number of lines that have a numeric conversion error
and discard those lines. You can do the error checking on input or in the
compareTo()
method.
Usage information:
java Sort [-i input] // specify input file [-o output] // specify output file [-l log_file] // specify log file [-f sort_fields] // specify sort fields and order [-F sort_field_file] // sort fields in: sort_field_file Sort the input file producing the output file. If the -l option is specified send errors to the log_file. Use the sort fields specified by -f or -F. java Sort [-h] Print usage information sort field specifier: [n][r]start_column-end_column[,...] n -- sort numerically. r -- reverse the order of the sort. fields are comma separated. No spaces are allowed. Examples: java Sort sort the standard input ascending as strings sending the output to the standard output. java Sort -i abc Sort file abc java Sort -f n50-60,r12-19 Sort the file numerically on columns 50 - 60. If their is a match, break the tie with a reverse alphabetic ordering of the field 12-19.
a 45 b -2 aaaaa b 3 b -3 aaaab c 4 b -4 aaaax d -6 a -2 aaaan x 17 a -3 aaaab n 4 a -4 aaaay
-f 1-25 // alpha sort all columns in order -f 1-5 // alpha sort first field -f r1-5 // alpha reverse sort first field -f n6-10 // numeric sort second field -f nr6-10 // numeric reverse sort second field -f 21-25 // alpha sort fifth field -f 11-15,1-5 // alpha sort third field, tie break on first -f 11-15,r1-5 // alpha sort third field, reverse tie break on first -f n16-19,n6-10 // numeric sort fourth field, tie break on second -f n16-19,11-15 // numeric sort fourth field, tie break on third
Please turn in a listing and an example program run. Test data will be provided.
![]() | ![]() | ![]() | 4.10 Week 10 -- I/O |