1 / 18

Reading and Writing Files in JAVA

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

draco
Télécharger la présentation

Reading and Writing Files in JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reading and Writing FilesinJAVA INFSY 535

  2. 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

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

  4. Reading Text Files • Scanner methods to read data from file • next, • nextLine, • nextInt, • nextDouble

  5. 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

  6. 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); }

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

  8. 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

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

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

  11. 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

  12. 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

  13. 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

  14. 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.

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

  16. 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

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

  18. 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.

More Related