DOC PREVIEW
UNI CS 4550 - More on Java Files

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Session 06 More on Java Files and HW #3Java File I/O Example: Echo.javaSlide 3Working with Standard Input and Output as FilesStreams vs. Readers and WritersSlide 6What can we do?A SolutionSlide 9EchoExerciseExercise - More ExamplesHomework #3 - Interacting with Fileshomework03-template.zipHW3: Task 1HW3: Task 2HW3: Task 3HW3: Task 4HW3: Task 5HW3: Task 6Session 06More on Java Filesand HW #3Java File I/O Example: Echo.java•echoes all the words in one file to an output file, one per line.$ java Echo hamlet.txt hamlet.out$ less hamlet.out1604thetragedyofhamletprinceofdenmarkbywilliamshakespeare ...import java.io.*;import java.util.StringTokenizer;public class Echo { public static void main( String[] args ) throws IOException { String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r"; BufferedReader inputFile = new BufferedReader(new FileReader(args[0]) ); PrintWriter outputFile = new PrintWriter( new FileWriter( args[1] ) ); String buffer = null; while( true ) { buffer = inputFile.readLine(); if ( buffer == null ) break; buffer = buffer.toLowerCase(); StringTokenizer tokens = new StringTokenizer( buffer, delimiters ); while( tokens.hasMoreElements() ) { String word = tokens.nextToken(); outputFile.println( word ); } // end while } // end while(true)... } // end main} // end class EchoWorking with Standard Input and Output as Files•Sometimes, we'd like to give the user an option of providing a file name or using standard I/O. •We can call sort with its own file argument, or we can pipe the standard output of one program (cat hamlet.out) as the standard input to sort. •How can we make our Java programs do the same thing?Streams vs. Readers and Writers•a stream is a device for transmitting or receiving a sequence of byte (8-bit) values–emphasis on reading/writing -- not on data itself–network and file systems are based on byte unit•Readers and Writers use 16-bit Unicode –useful for I/O of textual values as opposed to binary data such as images, colors, etc.–for example, BufferedRead has readLine methodWorking with Standard Input and Output as Files•Standard input is an instance of the InputStream class and does not respond to readLine(), which is how we would like to grab lines of text as Strings. •Standard output does respond to println() messages, but it is a PrintStream, which cannot be stored in a PrintWriter variable.What can we do?•We could write duplicate code for the four different cases. (file-file, file-stdout, stdin-file, stdin-stdout)•Every case would look the same except for one or two lines. •That doesn't seem to be the correct solution.•Maybe we can find a way to have them talk to objects that talk to standard input and output...A Solution•Let's take advantage of an object-oriented idea: We ought to be able to substitute an object with a common interface, even if somewhat different behavior, in place of one another, and let the new object fulfill the responsibilities of the replaced one.•While BufferedReaders and PrintWriters don't know how to talk to standard input and output, respectively, we can use a translator to serve as a go-between. •Java give us the classes we need: InputStreamReader and OutputStreamWriter.import java.io.*;import java.util.StringTokenizer;public class EchoStandard { public static void main( String[] args ) throws IOException { String delimiters = " .?!()[]{}|?/&\\,;:-\'\"\t\n\r"; BufferedReader inputFile = new BufferedReader( new InputStreamReader( System.in ) ); PrintWriter outputFile = new PrintWriter( new OutputStreamWriter( System.out ) ); String buffer = null; while( true ) { buffer = inputFile.readLine(); if ( buffer == null ) break; buffer = buffer.toLowerCase(); StringTokenizer tokens = new StringTokenizer(buffer,delimiters); while( tokens.hasMoreElements() ) { String word = tokens.nextToken(); outputFile.println( word ); } // end while } // end while( true )... } // end main} // end class EchoStandardEcho BufferedReader inputFile = new BufferedReader( new FileReader( args[0]) );PrintWriter outputFile = new PrintWriter( new FileWriter( args[1]) ); vs. EchoStandardBufferedReader inputFile = new BufferedReader( new InputStreamReader( System.in ) );PrintWriter outputFile = new PrintWriter( new OutputStreamWriter( System.out ) );Exercise• Turn Echo.java into EchoV2.java, which behaves just like Echo, except that it takes two optional command-line arguments: the names of the input file and output file, respectively. •If the user omits the second argument, the program writes to standard output. •If the user omits both arguments, the program reads from standard output and writes to standard output. For example: $ java EchoV2 hamlet.txt hamlet.out $ less hamlet.out 1604 the tragedy of ...Exercise - More Examples$ java EchoV2 EchoV2.java ...$ java EchoV2 hamlet.txt | less (interesting that the pipe “|” is not args[1])1604thetragedyof...$ java EchoV2...$ cat hamlet.txt | java EchoV2 | less1604thetragedyof...Homework #3 - Interacting with Files•Extend the MemoPad program so it can save and load memos to and from files•You’ll need to add two responsibilities to the MemoDatabase interface: –loading from a file –saving to a filehomework03-template.zip•a new MemoPadApp.java that only creates instances MyMemoDatabase. It accepts a single optional argument, the maximum size of the database. java MemoPadApp // creates a default student database java MemoPadApp 100 // creates a database with up to 100 memos•all of the graphics and management classes necessary to compile your application•a jar file that will let you run a working program, to see how your program should work. Just use this command: java -jar memopad.jar // same as first example above java -jar memopad.jar 100 // same as second example aboveHW3: Task 1•Change MemoPadApp.java to create instances of your MemoDatabase class.– On Lines 8 and 10, MemoPadApp creates instances of a MemoDatabase for the MemoPad. Put your class's name in place of my class's name.


View Full Document

UNI CS 4550 - More on Java Files

Download More on Java Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More on Java Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More on Java Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?