Reading and Writing Files in JAVA INFSY 535


















- Slides: 18

Reading and Writing Files in JAVA INFSY 535

Input � Input should be captured in String format � Programmer should check data for integrity ◦ Examining input character by character. ◦ A JAVA Exception � The programmer changes input to a desirable format

Reading Text Files 1. Reading text • Scanner class 2. Read from a disk file • construct a File. Reader • use the File. Reader to construct a Scanner object File. Reader reader = new file. Reader ("input. txt"); Scanner in = new Scanner(reader);

Reading Text Files Scanner methods to read data from file • next, • next. Line, • next. Int, • next. Double

A Sample Program • Reads all lines of a file and sends them to the output file, preceded by line numbers Sample input file: 012345678901234567890 01 Logan 002 Male 0175 03 Remy 004 Female 0100 08 Cole 045 Female 0005 02 Jace 008 Male 0086 05 Robert 002 Male 0025 07 Lynne 045 Female 0002

package scannerpkg; import java. io. File. Not. Found. Exception; import java. io. File. Reader; import java. io. IOException; import java. io. Print. Writer; import java. util. Scanner; public class Read. The. Data { Scanner console, in; String str; int line. Number; public Read. The. Data () throws File. Not. Found. Exception { console = new Scanner (System. in); str = console. next(); File. Reader f = new File. Reader (str); }

(cont. ) in = new Scanner (f); String line = in. next. Line(); while (in. has. Next. Line()) { String line = in. next. Line(); System. out. println("/* " + line. Number + " */ " + line); line. Number++; line = in. next. Line(); } }

� One line of data is read with one input statement � Either numeric data must be read one line at a time or � the exact column location must be known public Read. The. Data () throws File. Not. Found. Exception

Screen Output System. out. println (); System. out. print();

Screen Output System. out. println ("This is a quote"); System. out. print("Enter a number: "); System. out. println (var 1 + " " + " : Answer");

Buffers/Streams ◦ Reads or writes a block of information at one time ◦ Stores data in memory until we are prepared to use it ◦ System. out. flush (): immediately emptys buffer

File Output Writing Text Files §Similar to screen Output §Two classes are used 1)File. Writer class which has limited functionality and 2) Print. Writer supplies the print and println methods

File Output � Print. Writer out. File = null; <name of outputfile> = new Print. Writer (<filename>); - <name of outputfile> is defined by the programmer - <filename> represents the name of a file, e. g. out. txt � If the filename contains a '/' - use double '/' eg C: //Prog. Files//out. txt

File Output Print. Writer � Generates a text file � Supports only 2 methods: print() and println() � Data is converted to a String and output as a String value. � An End of Line (EOL) character is placed at the end of the line.

import java. io. File. Writer; import java. io. IOException; import java. io. Print. Writer; public class Test. Files { public static void main(String[] args) throws IOException { Print. Writer out. File = null; String line. One = "This tutorial covers File Processing"; String line. Two = "Note throw IOException"; String line. Three="Note the import statements"; out. File = new Print. Writer("outfile. dat”); out. File. println(line. Three); out. File. println(line. Two); out. File. println(line. One); out. File. close(); // Closes file and flush buffer } }

Lab � Given on next pages: ◦ tester class, create a project and run program ◦ Addressin. txt � Code File. Of. Addresses ◦ Constructor to open the file reader, scanner, and printwriter ◦ get. Line method ◦ print. Line method ◦ close. Files method

Throws clause § Ensures that: • Method is available to output an exception message §When exception/error occurs, the system outputs a message

substring method � To select part(s) of an input file line of data: format: line. substring (n 1, n 2); Note: use of substring with the String variable associated with line.