Presentation is loading. Please wait.

Presentation is loading. Please wait.

OO Design and Programming II I/O: Reading and Writing

Similar presentations


Presentation on theme: "OO Design and Programming II I/O: Reading and Writing"— Presentation transcript:

1 OO Design and Programming II I/O: Reading and Writing
5/12/2018 CS262

2 What I/O? Basics first Write output on the screen
Write output to a file Get input from the command line Get input from a file Get input from the screen (swing) As you can master the basics, move to advanced I/O 5/12/2018 CS262

3 File Class abstraction machine-independent
deals with most of the machine-dependent complexities of files and path names filename is a string Constructors Methods: canRead(); canWrite(); exists(); isFile(); isAbsolute(); 5/12/2018 CS262

4 5/12/2018 CS262

5 File Class Example Examples\BJ_FileClassTest
5/12/2018 CS262

6 I/O Stream Similar to C/C++ 5/12/2018 CS262

7 java.io package Reading Writing open a stream while more information
read information close the stream Writing open a stream while more information write information close the stream 5/12/2018 CS262

8 Character Streams and Byte Streams
What is the difference? 5/12/2018 CS262

9 Superclass Reader (character)
5/12/2018 CS262

10 Superclass Reader (character)
Reader and Writer - abstract superclasses for character streams Reader: API and partial implementation for readers Streams that read 16-bit characters Subclasses implement specialized streams Be familiar to searching specifications in the API documentation 5/12/2018 CS262

11 To read from a file Use int read() int read( ) throws IOException
Reads a single character from the file and returns an integer value (0 to 65535) Will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method. read( ) returns -1 when the EOF is encountered 5/12/2018 CS262

12 Superclass Writer (character)
5/12/2018 CS262

13 Reader 5/12/2018 CS262

14 Writer 5/12/2018 CS262

15 FileReader To construct a FileReader, use the following constructors:
public FileReader(String filename) public FileReader(File file) Ref: A java.io.FileNotFoundException would occur if you attempt to create a FileReader with a nonexistent file. 5/12/2018 CS262

16 How to read with FileReader
// Create an input stream input = new FileReader("temp.txt"); int code; /* Repeatedly read a character and display it on the console*/ while ((code = input.read()) != -1) System.out.print((char)code); 5/12/2018 CS262

17 // To catch I/O exceptions
try { // Create an input stream // Repeatedly read a character and display it on the console } catch (FileNotFoundException ex) { System.out.println("File temp.txt does not exist"); catch (IOException ex) { ex.printStackTrace(); finally { input.close(); // Close the stream 5/12/2018 CS262

18 FileWriter To construct a FileWriter, use the following constructors:
To construct a FileWriter, use the following constructors: public FileWriter(String filename) public FileWriter(File file) public FileWriter(String filename, boolean append) public FileWriter(File file, boolean append) See Examples\BJ_TestFileWriter.java 5/12/2018 CS262

19 InputStreamReader InputStreamReader converts byte streams to character streams: reads bytes and decodes them into characters using a specified charset. charset specifiable by name or given explicitly, or the platform's default charset 5/12/2018 CS262

20 Superclass Writer (character)
5/12/2018 CS262

21 InputStreamReader 5/12/2018 CS262

22 BufferedReader/BufferedWriter
For efficient I/O: BufferedReader: readLine() method If the end of stream is reached, readLine() returns null. BufferedWriter: newLine() method to write a line separator. 5/12/2018 CS262

23 Coding BufferedReader in =
new BufferedReader(new FileReader("foo.in")); buffers the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient 5/12/2018 CS262

24 Examples of BufferedReader
See BJ_BRread BJ_BRreadLines BJ_TinyEditor // reads multiple lines from console and reverberates using InputStreamReader and BufferedReader 5/12/2018 CS262

25 PrintWriter/PrintStream
BufferedWriter: outputs characters and strings. PrintWriter and PrintStream both output objects, strings and numeric values as text. PrintWriter in jdk1.2 and more efficient replaces PrintStream jdk1.0) 5/12/2018 CS262

26 PrintWriter To construct a PrintWriter: public PrintWriter(Writer out)
public PrintWriter(Writer out, boolean autoFlush) If autoFlush is true, the println methods will cause the buffer to be flushed. The methods in PrintWriter and PrintStream do not throw an IOException though some constructor may throw. No need to invoke them from a try-catch block for methods. Example: BJ_TestPrintWriter.java 5/12/2018 CS262

27 Binary I/O Text I/O: read: specific encoding to Unicode
write: Unicode to file specific encoding Binary I/O: No conversions; exact byte read or written to file 5/12/2018 CS262

28 Superclass InputStream (byte)
5/12/2018 CS262

29 Superclass OutputStream (byte)
5/12/2018 CS262

30 Using the Streams Input Output character FileReader FileWriter
PrintWriter binary FileInputStream FileOutputStream PrintStream PrintOutputStream

31 Q: What is the difference?
Open File for Reading File inFile = new File("test_in.txt"); FileReader in = new FileReader(inFile); File inFile = new File("test_in.txt"); FileInputStream in = new FileInputStream(inFile); Q: What is the difference? 5/12/2018 CS262

32 FileReader vs. FileInputStream
reads streams of characters FileInputStream reads streams of raw bytes example: image data Can also read characters See BJ_ShowFile // reads from file and displays 5/12/2018 CS262

33 Open File for Writing File outFile = new File("test_out.txt");
FileWriter out = new FileWriter( outFile ); File outFile = new File("test_out.txt"); FileOutputStream out = new FileOutputStream(outFile); File outFile = new File(“test_out.txt”); PrintStream outfile = new PrintStream(outFile); 5/12/2018 CS262

34 Close File Infile.close( ); outFile.close( ); 5/12/2018 CS262

35 Command Line Arguments
public static void main(String[ ] args) { if( args.length == 2 ) { String firstArgument = args[0]; String secondArgument = args[1]; } else { System.out.println(“java command <args1> <args2>”); } 5/12/2018 CS262

36 Source Code Review and Demo Copy Files
BJ_Copy_File project containing: CopyFileWOException and CopyfileWithException 5/12/2018 CS262

37 Combination of multiple input streams Source Code Review and Demo
BJ_Concatenate Exercise : run Concatenate.java Don’t forget ListOfFiles.java Add a println statement to trace program execution and make sure you understand the execution sequence 5/12/2018 CS262

38 Note the difference between
read() Return int Check for -1 to detect EOF readLine() Return a string Check for null to detect EOF 5/12/2018 CS262

39 read( ) int c = in.read( ); int c; while( (c = in.read()) != -1 ) {
……. } 5/12/2018 CS262

40 readLine( ) String str = infile.readLine( ); While( true ) {
if( str == null ) break; …… } 5/12/2018 CS262


Download ppt "OO Design and Programming II I/O: Reading and Writing"

Similar presentations


Ads by Google