Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT

Similar presentations


Presentation on theme: "CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT"— Presentation transcript:

1 CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
CSC 238 – Object Oriented Programming

2 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT Classes from java.io package: Classes File, FileWriter, BufferedWriter, PrintWriter Methods 1printf(String format, Object..args) –a convenience method to write a formatted string to this writer using the specified format string and arguments 2.print(x) –print x; x is a char, int,…, String or object 3.println(x) –print x & go to next line. x is a char, int,…, String or object 4.close() –close this output stream

3 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT Output classes public class FileWriter–convenience class for writing character files FileWriter fw = new FileWriter(“price.txt”)

4 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT Output classes public class BufferedWriter- write text to a character output stream, buffering characters so as to provide for the efficient writing of single characters, arrays and string. The buffer size may be specified or the default size may be accepted. Without buffering, each invocation of print method would cause characters will be converted into bytes that would then be written immediately to the file-inefficient. BufferedWriter bw = new BufferedWriter(fw);

5 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT Output classes public class PrintWriter–print formatted representations of objects to a text output stream. PrintWriter pw = new PrintWriter(bw); //prints formatted, buffered output to textfile; //will buffer the PrintWriter’s output to the file

6 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT

7 JAVA TEXT FILE CLASSES & METHODS
Steps in reading/writing a file 1. Make a file or FileWriter output object to link with an external file name Eg: FileWriter fw = new FileWriter(“file.out”); 2. Create a buffered, formatable Java output stream object and attached it to a file Eg: BufferedWriter bw = new BufferedWriter(fw); 3. PrintWriter pw = new PrintWriter(bw) 4. Write the contents of the file 5. Close the file pw.close();

8 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT (sample program)

9 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT (sample program)

10 JAVA TEXT FILE CLASSES & METHODS
Writing to text File/OUTPUT (sample program) cont

11 String Tokenizer Processing data in text file
Use StringTokenizer class to break up 1 line statement into words StringTokenizer is include in package java.util import java.util.*

12 String Tokenizer Processing data in text file

13 String Tokenizer Processing data in text file

14 String Tokenizer

15 Reading an input file with 2 different lines using String Tokenizer

16 String Tokenizer

17 Sample Program (Reading and Writing Text Files):

18 Sample Program (Reading and Writing Text Files):

19 Exception handling Exception handling is used to deal with runtime errors Exception may occur for various reasons such as: Entering an invalid input The program may attempt to open a file that doesn’t exist Network connection may hang up The program may attempt to access an out of bounds array element A Java exception is an instance of a class derived from Throwable class which contained in the java.lang package and subclasses of Throwable are contained in various packages

20 Some of Java’s Exception Classes

21 Handling exception 3 statements try
Identifies a block of statements within which an exception might be thrown catch Must be associated with a try statement and identifies a block of statements that can handle a particular type of exception occurs within the try block finally Used to execute some code if an exception occurs or is caught Must be associated with a try statement and identifies a block of statements that are executed regardless whether or not an error occurs within the try block

22 Handling exception

23 Exercise

24 Answer

25 Answer

26 Answer


Download ppt "CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT"

Similar presentations


Ads by Google