University of Central Florida COP 3330 Object Oriented Programming

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CSC – Java Programming II Lecture 9 January 30, 2002.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
Lecture 2 Objectives Learn about objects and reference variables.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
 Pearson Education, Inc. All rights reserved Files and Streams.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
28 Formatted Output.
Part I General Principles
OO Design and Programming II I/O: Reading and Writing
Ch14 Files and Streams OBJECTIVES
Lesson 8: More File I/O February 5, 2008
3 Introduction to Classes and Objects.
Objectives You should be able to describe: Interactive Keyboard Input
Program Input/Output (I/O)
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Chapter 2 part #3 C++ Input / Output
Chapter 13: File Input and Output
I/O Streams- Basics Byte Streams and Character Streams
Programming in Java Files and I/O Streams
Java Programming Course
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Streams and File I/O Chapter 14.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Lecture 13 Input/Output Files.
Chapter 3: Introduction to Objects and Input/Output
CSS 161: Fundamentals of Computing
Unit 6 Working with files. Unit 6 Working with files.
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Chapter 2 part #3 C++ Input / Output
Java Basics Data Types in Java.
Web Design & Development Lecture 8
14 Files and Streams.
Chapter 15 Files, Streams and Object Serialization
Presentation transcript:

University of Central Florida COP 3330 Object Oriented Programming

Agenda Input/Output

Basic Input / Output

Byte Streams Programs use byte streams to perform input and output of 8-bit bytes All byte stream classes are descended from classes InputStream and OutputStream Always close streams when it's no longer needed is very important Close streams in a finally block to guarantee that both streams will be closed even if an error occurs helps avoid serious resource leaks A possible error is that a file cannot be opened When that happens, the stream variable corresponding to the file never changes from its initial null value Make sure that each stream variable contains an object reference before invoking close

Byte Streams

Byte-based Stream

Character Streams Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set, usually an 8-bit superset of ASCII A program that uses character streams in place of byte streams automatically adapts to the local character set All character stream classes are descended from classes Reader and Writer

Character Streams

Character-based Stream

Line-based I/O Character I/O usually occurs in bigger units than single characters One common unit is the line (i.e. a string of characters with a line terminator at the end) A line terminator can be carriage-return/line-feed sequence ("\r\n") a single carriage-return ("\r") a single line-feed ("\n") Supporting all possible line terminators allows programs to read text files created on any of the widely used operating systems

Line-based I/O

Buffered Streams Examples so far use unbuffered I/O, each read or write request is handled directly by the underlying OS Can result in a program being much less efficient Each such request often triggers disk access, network activity, or some other operation that is relatively expensiv Java platform implements buffered I/O streams Buffered input streams read data from a memory area known as a buffer Buffered output streams write data to a buffer inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new BufferedWriter(new FileWriter("characteroutput.txt")); Four buffered stream classes used to wrap unbuffered streams:  BufferedInputStream and BufferedOutputStream create buffered byte streams BufferedReader and BufferedWriter create buffered character streams

Buffered Streams Flushing Buffered Streams Write out a buffer at critical points, without waiting for it to fill, known as flushing the buffer Some buffered output classes support autoflush, specified by an optional constructor argument When autoflush is enabled, certain key events cause the buffer to be flushed To flush a stream manually, invoke its flush method

Scanning Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type By default, a scanner uses white space to separate tokens Even though a scanner is not a stream, close it to indicate being done with its underlying stream To use a different token separator, invoke useDelimiter(), specifying a regular expression

Scanning All input tokens treated as simple String values Scanner also supports tokens for primitive types (except for char) BigInteger BigDecimal Numeric values can use thousands separators therefore in US locale, Scanner correctly reads the string "32,767" as representing an integer value. Locale is an issue because thousands separators and decimal symbols are locale specific Not all scanning would work correctly in all locales without specifying scanner should use the US locale

Scanning

Formatting Stream objects that implement formatting are instances of PrintWriter ,a character stream class, PrintStream, a byte stream class, most common classes used System.out System.err print and println  format individual values in a standard way outputs a single value after converting the value using the appropriate toString method format formats almost any number of values based on a format string, with options for precise formatting

Formatting There are many conversions, GTS! The format method formats multiple arguments based on a format string The format string consists of static text embedded with format specifiers d formats an integer value as a decimal value. f formats a floating point value as a decimal value. n outputs a platform-specific line terminator. x formats an integer as a hexadecimal value. s formats any value as a string. tB formats an integer as a locale-specific month name. There are many conversions, GTS!

Formatting A format specifier can contain several additional elements that further customize the formatted output; this uses every possible kind of element

Formatting Precision. For floating point values, this is the mathematical precision of the formatted value. For s and other general conversions, this is the maximum width of the formatted value; the value is right-truncated if necessary. Width. The minimum width of the formatted value; the value is padded if necessary. By default the value is left-padded with blanks. Flags specify additional formatting options. In the Format example, the + flag specifies that the number should always be formatted with a sign, and the 0 flag specifies that 0 is the padding character. Other flags include - (pad on the right) and , (format number with locale-specific thousands separators). Note that some flags cannot be used with certain other flags or with certain conversions. The Argument Index allows you to explicitly match a designated argument. You can also specify < to match the same argument as the previous specifier.

Formatting

File Input / Output

Files and Streams A file system stores and organizes files on some form of media in a retrievable manner Most file systems store the files in a tree (or hierarchical) structure with a root node at the top with there are files and directories (folders)

Files and Streams A path is relative or absolute Why files? Absolute path always contains the root element and the complete directory list required to locate the file /home/sally/statusReport Relative path needs to be combined with another path in order to access a file joe/foo is a relative path Why files? Variables and arrays data storage is temporary For persistent data maintain in files or databases Files are a sequential stream of bytes Every operating system has some methodology for identifying end of a file end-of-file marker Total byte count

Standard I/O Using java.io Package

JFileChooser Displays dialog to allow the user to select a file or directory