SlideServe
Menu
Préférences
© SlideServe · v2026

Chapter 15 Text Processing and File Input/Output

kyleigh avatar
20 diapositives · 391 Views · Oct 28, 2012

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, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: describe the java.io package

Chapter 15 Text Processing and File Input/Output - slide 1
01 / 20Chapter 15 Text Processing and File Input/Output
1 / 17

Télécharger la présentation

Chapter 15 Text Processing and File Input/Output
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Chapter 15Text Processing andFile Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

  2. Chapter Preview In this chapter we will: • describe the java.io package • introduce the Java StringBuffer class • show how files can be read and written • discuss how to handle file input and output exceptions • demonstrate how to perform console input and output

  3. Strings • Java provides a number of methods for operating on String objects • String objects are immutable • Immutable objects cannot be changed once they are created

  4. StringBuffer • Java provides a mutable staring class called StringBuffer that allows strings to grow dynamically during program execution • Several StringBuffer methods are the same as those found in String • To convert between String objects and StringBuffer objects Java provides constructors for each class • The StringBuffer class also contains a ToString method to allow easier output

  5. Sequential Files • Files are stored are stored on disks • In this section we will assume that files consist of multiple lines composed of characters • Each line ends with an end of line character • The file itself may have an end of file character • Programmers often need to read or write files stored on disks

  6. File Input • Java classes that support file input are found in the java.io package • FileReader allows us to open a file for reading • BufferedReader is a wrapper class that provides methods that • allow us to treat the file as a stream of characters • increases the efficiency of reading • allows line-oriented reading

  7. Wrapper Classes • Class W is said to wrap class Y if: • Y is a concrete (not abstract) class • W’s constructor takes Y as an argument and stores a local copy of Y • W reimplements all of Y’s methods • A wrapper can wrap a class and be the subclass of another class at the same time

  8. Buffered Input • We can make a file available for reading one character at a time by using FileReader fr = FileReader(filename); • We can read the file more efficiently by reading a block of characters at a time, this is called buffering the read • To perform a buffered read we use this BufferedReader br = new BufferedReader( new FileReader(filename));

  9. Input Example – Part 1 import CSLib.*; import java.io.*; public class Copy { // Copy file to an OutputBox private FileBuffer fr; private BufferedReader br; public Copy(String filename) throws IOException { // open local file given by filename br = new BufferedReader(new FileReader(filename)); }

  10. Input Example – Part 2 public void copy( ) throws IOException { OutputBox out = new OutputBox( ); int I; while (true) { i = br.read(); if (i == -1) // check for end of file return; out.print((char) i); // must have char to print } } }

  11. File Output • Java classes that support file output are found in the java.io package • FileWriter and BufferedReader provide methods to write • a single character • an array of characters • a string • the end of line • PrintWriter is a wrapper class provided to convert several data type values to printable forms

  12. Writing to a File • To make a file available for printing PrintWriter pr = new PrintWriter( new BufferedWriter( new FileWriter(filename))); • PrintWriter has void methods print and println that behave the same way as they for OutputBox objects

  13. Mail Merge Application – part 1 import java.io.*; public class MailMerge{ // mail merge application BufferedReader template, maillist; PrintWriter out; public void openFiles(String fn) throws Ioexception { template = new BufferedReader( new FileReader(fn + “.template”)); maillist = new BufferedReader( new FileReader(fn + “.list”)); out = new PrintWriter( new BufferedWriter( new FileReader(fn + “.out”))); }

  14. Mail Merge Application – part 2 private StringBuffer readUpto(BufferedReader br, char delim) throws Ioexception { StringBuffer sb = new StringBuffer( ); int inputChar; while (true) { inputchar = br.read( ); if ((inputchar == -1) || (inputchar == delim)) return sb; sb.append((char) inputchar); } } private StringBuffer[] st = new StringBuffer[10];

  15. Mail Merge Application – part 3 private int readTemplate( ) throws Ioexception { int n; // Read the first portion of the template st[0] = readUpro(template, ‘%’); // If there is there is an ‘%’ as the first char // read the rest of the template for (n = 1; n < 10; n++) { st{n] = readUpto(template, ‘%’); if (st[n].length() == 0) // might be empty return n; } return n; }

  16. Mail Merge Application – part 4 public void merge( ) throws Ioexception { Stringbuffer sm; in n = readTemplate(); // Read the first field sm = readUpto(mailList, ‘#’); while (true) { // if no more fields if (sm.length() == 0)return; out.print(st[0]); // interleave template portions and fields for (int i = 1; i < n; i++) { mailList.read( ); // get past new line out.print(sm); // print field out.print(st[i]); // print next template sm = readUpto(mailList, ‘#’); } out.println(“--------------------------------”); }

  17. Mail Merge Application – part 5 public void closeFiles( ) { out.close( ); } } • A typical client import java.io.*; public class MailMergeClient { public static void main (String{] args) throws exception IOException { MailMerge mm = new MailMerge( ); mm.openFiles(args[0]); mm.merge( ); mm.closeFiles( ); } }