Chapter 15 - Text processing and file Input/Output. 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.
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 • 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 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 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 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 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 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 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)); Input Example – Part 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)); } Input Example – Part 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 } } } 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 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 Mail Merge Application – part 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”))); } Mail Merge Application – part 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]; Mail Merge Application – part 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; } Mail Merge Application – part 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(“ ”); } Mail Merge Application – part 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( ); } } .. .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... 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... reading – allows line-oriented reading 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