Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class I/O ObjectsI/O Objects Lab 12Lab 12.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class I/O ObjectsI/O Objects Lab 12Lab 12."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class I/O ObjectsI/O Objects Lab 12Lab 12

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota The Lab Problem You have some objects alreadyYou have some objects already –System.in = a sort of built-in File object –System.out = a sort of built-in FileWriter object You need some moreYou need some more –Scanner for System.in –File for the input file (to see if it exists) –Scanner for the input file –FileWriter for the output file You need Strings, tooYou need Strings, too –One for the “stub” of the file name –You might want others, but they aren’t essential

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota The two methods Main does most of the workMain does most of the work –It has most of the objects –You pass two objects to CopyFile CopyFile is very small and simpleCopyFile is very small and simple –Just a loop till nothing more in the Scanner object –Scan in a line to a String object –Writeln the line out to the FileWriter object

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota Dealing with the input File fn; Scanner kb = new Scanner(System.in); String nametxt; do { nametxt = kb.nextLine(); fn = new File (nametxt+”.txt”); } while (!fn.exists());

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota Problems I’ve seen Using Strings instead of I/O objectsUsing Strings instead of I/O objects –Some will also take strings, but that’s not what we need here –The File object lets us check for existing file Scanner does notScanner does not Using the wrong ScannerUsing the wrong Scanner –Input “waits” in the command shell –You type some text, type Control-C –The text shows up in your file Questions??Questions??

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class I/O ObjectsI/O Objects Lab 12Lab 12."

Similar presentations


Ads by Google