20
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 15 Text Processing and File Input/Output

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 15 Text Processing and File Input/Output

Chapter 15Text 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

Page 2: Chapter 15 Text Processing and File Input/Output

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

Page 3: Chapter 15 Text Processing and File Input/Output

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

Page 4: Chapter 15 Text Processing and File Input/Output

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

Page 5: Chapter 15 Text Processing and File Input/Output

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

Page 6: Chapter 15 Text Processing and File Input/Output

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

Page 7: Chapter 15 Text Processing and File Input/Output
Page 8: Chapter 15 Text Processing and File Input/Output

Wrapper Classes

• Class W is said to wrap class Y if:1. Y is a concrete (not abstract) class

2. W’s constructor takes Y as an argument and stores a local copy of Y

3. W reimplements all of Y’s methods

• A wrapper can wrap a class and be the subclass of another class at the same time

Page 9: Chapter 15 Text Processing and File Input/Output

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));

Page 10: Chapter 15 Text Processing and File Input/Output

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));

}

Page 11: Chapter 15 Text Processing and File Input/Output

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 } }}

Page 12: Chapter 15 Text Processing and File Input/Output

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

Page 13: Chapter 15 Text Processing and File Input/Output
Page 14: Chapter 15 Text Processing and File Input/Output

Writing to a File

• To make a file available for printingPrintWriter 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

Page 15: Chapter 15 Text Processing and File Input/Output
Page 16: Chapter 15 Text Processing and File Input/Output

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”))); }

Page 17: Chapter 15 Text Processing and File Input/Output

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];

Page 18: Chapter 15 Text Processing and File Input/Output

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;

}

Page 19: Chapter 15 Text Processing and File Input/Output

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(“--------------------------------”); }

Page 20: Chapter 15 Text Processing and File Input/Output

Mail Merge Application – part 5 public void closeFiles( ) { out.close( );

}

}

• A typical clientimport 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( );

}

}