540 likes | 1.54k Views
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. Reading text
E N D
Reading and Writing FilesinJAVA 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 • Reading text • Scannerclass • Read from a disk file • construct a FileReader • use the FileReader to construct a Scannerobject • FileReader reader = new fileReader ("input.txt"); • Scanner in = new Scanner(reader);
Reading Text Files • Scanner methods to read data from file • next, • nextLine, • nextInt, • nextDouble
A Sample Program • Reads all lines of a file and sends them to the output file, preceded by line numbers • Sample input file: • 01234567890123456789012345678901234567890 • 01Logan 002Male 0175 • 03Remy 004Female 0100 • 08Cole 045Female 0005 • 02Jace 008Male 0086 • 05Robert 002Male 0025 • 07Lynne 045Female 0002
package scannerpkg; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class ReadTheData { Scanner console, in; String str; intlineNumber; public ReadTheData () throws FileNotFoundException { console = new Scanner (System.in); str = console.next(); FileReader f = new FileReader (str); }
(cont.) in = new Scanner (f); String line = in.nextLine(); while (in.hasNextLine()) { String line = in.nextLine(); System.out.println("/* " + lineNumber + " */ " + line); lineNumber++; line = in.nextLine(); } }
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 ReadTheData () throws FileNotFoundException
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 (var1 + " " + " :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)FileWriter class which has limited functionality and 2) PrintWriter supplies the print and println methods
File Output • PrintWriteroutFile = null; <name of outputfile> = new PrintWriter (<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://ProgFiles//out.txt
File Output PrintWriter • 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.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class TestFiles { public static void main(String[] args) throws IOException { PrintWriteroutFile = null; String lineOne = "This tutorial covers File Processing"; String lineTwo = "Note the throw IOException"; String lineThree="Note the import statements"; outFile = new PrintWriter("outfile.dat”); outFile.println(lineThree); outFile.println(lineTwo); outFile.println(lineOne); outFile.close(); // Closes file and flush buffer } }
Lab • Given on next pages: • tester class, create a project and run program • Addressin.txt • Code FileOfAddresses • Constructor to open the file reader, scanner, and printwriter • getLine method • printLine method • closeFiles 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 (n1,n2); Note: use of substring with the String variable associated with line.