CS 202 Java Object Oriented Programming Command Line

CS 202 Java Object Oriented Programming Command Line Parameters and File I/O Chengyu Sun California State University, Los Angeles
![Command Line Parameters of main() n public static void main( String args[] ) java Command Line Parameters of main() n public static void main( String args[] ) java](http://slidetodoc.com/presentation_image_h2/c15bff635893f048e552cfe77b83c1f7/image-2.jpg)
Command Line Parameters of main() n public static void main( String args[] ) java Classname <arg 0> <arg 1> … Netbeans n Build –> Set Arguments …

CLP Example Add up a list of integers from user input public class Add { public static void main( String args[] ) { int sum = 0; for( int i=0 ; i < args. length ; ++i ) sum += Integer. parse. Int(args[i]); } System. out. println(sum); } // end of class Add

Streams

Stream Types Character streams n n Textual information Handled by Reader and Writer classes Byte streams n n Binary information Handled by Input. Stream and Output. Stream classes

Reader Classes Buffered. Reader Line. Number. Reader Byte. Array. Reader Input. Stream. Reader File. Reader Filter. Reader Pushback. Reader Piped. Reader String. Reader

Writer Classes Buffered. Writer Byte. Array. Writer Output. Stream. Writer Filter. Writer Piped. Writer String. Writer File. Writer

Input. Stream Classes Input. Stream File. Input. Stream Line. Number. Input. Stream Piped. Input. Stream Data. Input. Stream Filter. Input. Stream Buffered. Input. Stream Byte. Array. Input. Stream Pushback. Input. Stream Sequence. Input. Stream String. Buffer. Input. Stream Object. Input. Stream

Output. Stream Classes File. Output. Stream Piped. Output. Stream Data. Output. Stream Filter. Output. Stream Buffered. Output. Stream Byte. Array. Output. Stream Print. Stream Object. Output. Stream

Basic Streams by Source/Destination Files n File. Reader/Writer/Input. Stream/Output. Stream Threads n Piped. Reader/Writer/Input. Stream/Output. Stream Memory n n n Byte. Array. Reader/Writer/Input. Stream/Output. Stream String. Reader/Writer String. Buffer. Input. Stream General n n Input. Stream, Input. Stream. Reader Output. Stream, Output. Stream. Writer

Basic Stream Operations Basic streams only recognize bytes or characters Operations n n Read/write a single byte or character Read/write an array of bytes or characters Inconvenient

Wrapper Streams by Function Data conversion n Data. Input. Stream/Output. Stream Printing n Print. Stream Buffering n Buffered. Reader/Writer/Input. Stream/Output. Stream Object serialization n Object. Input. Stream/Output. Stream Others

Important Wrapper Streams and Operations Data. Input. Stream and Data. Output. Stream n n n Read and write primitive types read. Int(), read. Double(), … write. Int( int i ), write. Doube( double d ), … Buffered. Reader n read. Line() Buffered. Writer n write( String s )

How to Choose from Stream Classes Step 1: Is the data in binary form or textual form? n n Binary: Input/Output. Stream Textual: Reader/Writer Step 2: What’s the data source or data destination? n Files, threads, memory, general Step 3: How to process the data? n Primitive data types, buffering, …

“Wrapping” Examples // buffered text file read/write Buffered. Reader br = new Buffered. Reader( new File. Reader(“file”) ); Buffered. Writer bw = new Buffered. Writer( new File. Writer(“file”) ); // un-buffered binary file read/write Data. Input. Stream di = new Data. Input. Stream( new File. Input. Stream(“file”) ); Data. Output. Stream do = new Data. Output. Stream( new File. Output. Stream(“file”) ); // buffered binary file read/write Data. Input. Stream di 2 = new Data. Input. Stream( new Buffered. Input. Stream( new File. Input. Stream() ) ); Data. Output. Stream do 2 = new Data. Output. Stream( new Buffered. Output. Stream( new File. Output. Stream() ) );

File Input Example Read from a file in the following format, and sum up all numbers 31 22 23 79 -10 20 33 33 1 30 – 1 44 43 45
![Get The File Name public static void main( String args[] ) { if( args. Get The File Name public static void main( String args[] ) { if( args.](http://slidetodoc.com/presentation_image_h2/c15bff635893f048e552cfe77b83c1f7/image-17.jpg)
Get The File Name public static void main( String args[] ) { if( args. length == 0 ) { System. err. println( “usage: java Sum <filename>” ); System. exit(1); } } // do something with args[0]

Paths Windows n Absolute path Unix n w c: pathtofile n Relative path w pathtofile Absolute path w /path/to/file n Relative path w path/to/file File separators – “/”, “\”, File. separator

Read In Each Line File. Reader fr = new File. Reader( filename ); // wrapping Buffered. Reader br = new Buffered. Reader( fr ); String line; while( (line = br. read. Line()) != null ) { // do something with s }

Break A Line Into Tokens String. Tokenizer st = new String. Tokenizer(line); while( st. has. More. Tokens() ) { int value = Integer. parse. Int( st. next. Token() ); } // add value to sum

A Few More Things I/O Classes are in the java. io package n import java. io. *; String. Tokenizer is in the java. util package n import java. util. *; File operations throw all kinds of exceptions n n Catch them, or Throw them Always remember to close a stream

File Class Not directly related to I/O Check file status: n n is a file or a directory exist, readable, writable name, path, parent length

Binary File vs. Text File If we can save data in either binary or text form, which one do we choose? n n n File size Convenience Speed Either way, always use buffering!

Random Access File The problem with the stream model Advantages of Random. Access. File n n n Deal with both binary and text files Provide both read and write methods seek(long pos) … but you’ll probably never use it. Why?
- Slides: 24