Georgia Institute of Technology Creating and Modifying Text part 4 Barb Ericson Georgia Institute of Technology Oct 2005.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Lecture 15: I/O and Parsing
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
ARRAYS public class ArrayDemo { public static void main(String[] args) { int[] anArray; // declare an array of integers anArray = new int[10]; // create.
Files and Streams CS 21a Chapter 11 of Horstmann.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)
Two Ways to Store Data in a File Text format Binary format.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO Basics By Dan Fleck Coming up: Data Streams.
CPSC1301 Computer Science 1 Chapter 12 Creating and Modifying Text part 4.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Georgia Institute of Technology Movies Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Lecture 5 I/O and Parsing
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Movies part 4 Barb Ericson Georgia Institute of Technology April 2006.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Georgia Institute of Technology Making Text for the Web part 2 Barb Ericson Georgia Institute of Technology March 2006.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology Creating and Modifying Text Barb Ericson Georgia Institute of Technology June 2005.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
For Friday Finish reading chapter 9 WebCT quiz 17.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
Georgia Institute of Technology Making Text for the Web part 3 Barb Ericson Georgia Institute of Technology March 2006.
OO Design and Programming II I/O: Reading and Writing
File Input / Output.
Lesson 8: More File I/O February 5, 2008
Creating and Modifying Text part 2
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
File class File myFile=new File(“c:/javaDemo/aa
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Reading and Writing Text Files
Unit 6 Working with files. Unit 6 Working with files.
Workshop for Programming And Systems Management Teachers
Workshop for Programming And Systems Management Teachers
Making Text for the Web part 6
Creating and Modifying Text part 3
Barb Ericson Georgia Institute of Technology May 2006
Presentation transcript:

Georgia Institute of Technology Creating and Modifying Text part 4 Barb Ericson Georgia Institute of Technology Oct 2005

Georgia Institute of Technology Learning Goals Media Goals –How to generate a form letter Computing concepts –How to write to a file –How to force a new line in the output –How to write a program that modifies another program

Georgia Institute of Technology Writing to a File Very similar to reading from a file –But use FileWriter and BufferedWriter –Write out things with the method write(string); –Force a new line with newLine(); –Different systems use different ways to end a line »Macs versus Windows –This will write it out in away that works for the current system

Georgia Institute of Technology SimpleWriter public class SimpleWriter { /** * Method to write a silly file */ public void writeSillyFile() { try { // try to open the buffered writer BufferedWriter writer = new BufferedWriter(new FileWriter("silly.txt")); // write out the file writer.write("Here is some text."); writer.newLine(); writer.write("Here is some more."); writer.newLine();

Georgia Institute of Technology Simple Writer - Continued writer.write("And now we're done."); writer.newLine(); writer.write("THE END"); writer.close(); } catch (Exception ex) { System.out.println("Error during write of silly.txt"); } public static void main(String[] args) { SimpleWriter writer = new SimpleWriter(); writer.writeSillyFile(); } }

Georgia Institute of Technology Generating a Form Letter You can use a method to personalize a form letter –By passing in the title, last name, city and eye color –And writing out the letter with these items inserted at the appropriate places

Georgia Institute of Technology Form Letter Generator Class import java.io.*; /** * Class used to generate form letters Barbara Ericson */ public class FormLetterGenerator { /** * Method to generate a form letter title the person's title (Mr., Mrs., Dr.) lastName the last name for the recipient city the name of the city for the recipient eyeColor the eye color of the recipient */ public void writeLetter(String title, String lastName, String city, String eyeColor) {

Georgia Institute of Technology Form Letter Generator Class - Cont String fileName = lastName + "Letter.txt"; // try to open the file and write to it try { // create the buffered writer to use to write the file BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); // write the beginning of the letter writer.write("Dear " + title + " " + lastName + ", "); writer.newLine();

Georgia Institute of Technology Form Letter Generator Class - Cont // write the body of the letter writer.write("I am writing to remind you of the offer"); writer.newLine(); writer.write("that we sent to you last week. Everyone in"); writer.newLine(); writer.write(city + " knows what an exceptional offer this is!"); writer.newLine(); writer.write("(Especially those with lovely eyes of " + eyeColor + "!)"); writer.newLine(); writer.write("We hope to hear from you soon."); writer.newLine();

Georgia Institute of Technology Form Letter Generator Class - Cont // write the ending writer.write("Sincerely,"); writer.newLine(); writer.write("I. M. Acrook"); // close the file writer.close(); } catch (Exception ex) { System.out.println("Error writing to " + fileName); } public static void main(String[] args) { FormLetterGenerator formGenerator = new FormLetterGenerator(); formGenerator.writeLetter("Mr.","Guzdial","Decatur","brown"); } }

Georgia Institute of Technology Write a File Exercise Create another method to write a form letter –Have it take the high temp, low temp and, chance of rain –Have it print out the following: –Todays high will be (high temp) and the low will be (low temp). There is a (chance of rain) % chance of rain

Georgia Institute of Technology Modifying a Program You can read the source code from a file –And change it in some way And write it back out Just read each line and look for a string that you want to change –If the current line doesn’t have the string to change then just add it to a list of lines –If the current line has the string to change then change it and add it to a list of lines When you reach the end of the file –Write out the lines in the list

Georgia Institute of Technology Modifying the Cartoon Class We will create a method to change the text passed to the addWordBalloon method –In the main method –Use indexOf to look for the text

Georgia Institute of Technology FileModifier Class import java.util.*; import java.io.*; /** * Class to demonstrate using a program to modify another program Barb Ericson */ public class FileModifier { /** * Method to modfiy the first string in a method to * be the passed changed text fileName the file name for the class to modify textToChange the text to change changedText the new text to use for the text to * change */ public void modifyFile(String fileName, String textToChange, String changedText)

Georgia Institute of Technology File Modifier - Cont { List lineList = new ArrayList(); String line = null; int pos = 0; // try the following try { // open the file to read from BufferedReader reader = new BufferedReader(new FileReader(fileName)); /* loop while there are more lines in the file * and we haven't found the text to change yet */ while((line = reader.readLine()) != null && line.indexOf(textToChange) < 0) lineList.add(line);

Georgia Institute of Technology File Modifier - Cont /* If we get there we either ran out of lines or we * found the text to change */ if (line != null) { // get the position of the text to change pos = line.indexOf(textToChange); // modify the string lineList.add(line.substring(0,pos) + changedText + line.substring(pos + textToChange.length())); // loop till the end of the file adding the rest while ((line = reader.readLine()) != null) { lineList.add(line); }

Georgia Institute of Technology File Modifier - Cont // now close the file reader.close(); // create a writer to write out the file BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); // loop writing out the lines for (int i = 0; i < lineList.size(); i++) { writer.write((String) lineList.get(i)); writer.newLine(); }

Georgia Institute of Technology File Modifier - Cont // close the writer writer.close(); } catch (FileNotFoundException ex) { SimpleOutput.showError("Couln't find file " + fileName); fileName = FileChooser.pickAFile(); modifyFile(fileName,textToChange,changedText); } catch (Exception ex) { SimpleOutput.showError("Error during read or write"); ex.printStackTrace(); }

Georgia Institute of Technology File Modifier – Main Method // Main method to run public static void main(String[] args) { FileModifier fileMod = new FileModifier(); String file = "C:\\intro-prog-java\\bookClassesFinal\\Cartoon.java"; fileMod.modifyFile(file, "Just Horsing Around!", "What's up, Wilbur?"); }

Georgia Institute of Technology Summary To write to a file –Use a BufferedWriter with a FileWriter Use write(String string) to write out a string Use newLine() to force a new line You can personalize a form letter –By passing in to a method the personal items One program can even modify another –The source code is just text –It will need to be compiled after it is changed