File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Similar documents
Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

PIC 20A Streams and I/O

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

CS 251 Intermediate Programming Java I/O Streams

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Simple Java Input/Output

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Dining philosophers (cont)

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Objec&ves. Review. Standard Error Streams

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Java Console Input/Output The Basics

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Programming Languages and Techniques (CIS120)

Lecture 11.1 I/O Streams

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

Software Practice 1 - File I/O

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Example: Copying the contents of a file

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Programming Languages and Techniques (CIS120)

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Programming Languages and Techniques (CIS120)

Java Input/Output. 11 April 2013 OSU CSE 1

Experiment No: Group B_4

Programming Languages and Techniques (CIS120)

CSC 1214: Object-Oriented Programming

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Byte and Character Streams. Reading and Writing Console input and output

CS Programming I: File Input / Output

Object Oriented Design with UML and Java. PART VIII: Java IO

Object-Oriented Programming in the Java language

CS Programming I: File Input / Output

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Jonathan Aldrich Charlie Garrod

HST 952. Computing for Biomedical Scientists Lecture 8

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

Introduction Unit 4: Input, output and exceptions

C17: I/O Streams and File I/O

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Object-Oriented Programming Design. Topic : Streams and Files

I/O Streams. Object-oriented programming

Sri Vidya College of Engineering & Technology Question Bank

CS18000: Programming I

I/O streams. Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

IT101. File Input and Output

Text User Interfaces. Keyboard IO plus

Exceptions and Working with Files

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

ITI Introduction to Computer Science II

Full file at

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Data Structures. 03 Streams & File I/O

Darshan Institute of Engineering & Technology for Diploma Studies

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lesson 3: Accepting User Input and Using Different Methods for Output

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Sorting/Searching and File I/O

Programming Languages and Techniques (CIS120)

Software 1. Java I/O

Full file at

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University

Each command-line argument is placed in the args array that is passed to the static main method as below :

H212 Introduction to Software Systems Honors

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

1.00 Lecture 30. Sending information to a Java program

Programming for Mobile Computing

File Processing in Java

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

File I/O Array Basics For-each loop

Another Example. Other Constructs

I/O STREAM (REQUIRED IN THE FINAL)

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Program Elements -- Introduction

Techniques of Java Programming: Streams in Java

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

Lab 10: Sockets 12:00 PM, Apr 4, 2018

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Transcription:

File Input/Output Tuesday, July 25 2006 CSE 1020, Summer 2006, Overview (1): Before We Begin Some administrative details Some questions to consider The Class Object What is the Object class? File Input and Output Overview Introduction File I/O Overview (2): Input/Output Streams Introduction Input Output As an Aside Code examples

Before We Begin Administrative Details (1): Some Reminders Lab Test 3 Tuesday, August 1 Last regularly scheduled lecture Thursday, August 3 will include exam review Final Exam Wednesday, August 16, 2006 7:00pm 10:00pm Vari Hall (VH) 3006 Some Questions to Consider (1): What is the Object class? Why have the Object class? What are some of the methods in the Object class? What happens if a method of the Object class is not overriden? What are the disadvantages of prompting the user for any required input? How does accessing input from a file overcome these disadvantages? How can we access the contents of a file?

File Input/Output Overview (1): About 100 Classes Dedicated to Input/Output (I/O) in the Java Library Located in the following packages java.io & java.nio Allow clients to delegate all I/O related operations We have actually already worked with I/O streams Scanner/PrintStream classes to read user input via the keyboard and to display to the screen We will now expand on our knowledge of this to cover I/O with files I/O with files allows for much more flexibility Introduction to File I/O (1): Programs Involving File I/O Typically start by prompting the user to enter the name of a file Can be done by simply the name of the file entered by the user as a String or You can use the standard file dialog box JFileChooser to allow the user to navigate through the file directory system and choose the desired file presents the user with a dialog window similar to what you see when you choose the Open menu option in a standard Windows application

Introduction to File I/O (2): Programs Involving File I/O (cont.) Open file dialog using JFileChooser JFileChooser filechooser = new JFileChooser(); filechooser.showopendialog(null); Part of the javax.swing package must have the following statement import javax.swing.* Introduction to File I/O (3): Programs Involving File I/O (cont.) Once the open file dialog window is displayed, the dialog will wait until the user takes an action Once the user performs an action (clicks on one of the button Open or Cancel, the program must examine the value of the reference obtained using the following method call chooser.getselectedfile(); If null user selected the Cancel option If not null invoke tostring to obtain file name Introduction to File I/O (4): Programs Involving File I/O (cont.) Example code segment related to getselectedfile if(chooser.getselectedfile()!= null) { String filename; filename = chooser.getselectedfile().tostring(); rest of program here } User doesn t have to explicitly choose a file by clicking on it in the file dialog can also enter the name of the file directly in the file name text field

File Input (1): Abstraction at Work Once the file name has been obtained, we can access its contents for reading in a simple manner making use of the familiar Scanner class Simply create an instance of the Scanner class passing the file name to its constructor as opposed to System.in Once the Scanner object is created then we use it as we normally do We do not have to be concerned with where the data is coming from Java hides that from us and allows for a common interface File Input (2): Abstraction at Work Creating the Scanner object to access a file for reading Of course, we can also create a Scanner object to handle keyboard input if we like both can be available to us at the same time Scanner fileinput = new Scanner(fileName); Scanner input = new Scanner(System.in); File Input (3): Reading From a File Similar to reading from the keyboard except for two differences No prompt is involved we do not have to prompt the user to enter anything since all the input already exists in the file Sentinels (character(s) indicating user is done are not required the file contains a hidden marker called end of file after its last field to indicate there is no more information contained in the file and we cannot read beyond it

File Input (4): Reading From a File (cont.) Scanner class contains a boolean method called hasnextx() that indicates whether the file contains more fields (of type X e.g., for int, use hasnextint) If all the fields in the file are of the same type (lets say int for example), then we can read its entire contents as follows while(fileinput.hasnextint()) { int x = fileinput.nextint(); } File Input (5): Reading From a File (cont.) Of course, we are not restricted to reading integers Can read any type including String when our file is a text-based document for example nextline You must be careful when mixing the reads between Strings and other primitive types Consider a file with the data 15\nText and the following calls will they work (e.g., will the int I be equal to 15 and the String s equal to Text?) int i = fileinput.nextint(); String s = fileinput.nextline(); File Output (1): Writing Data to a File Very similar to writing data to the display (screen) Instead of creating a PrintStream object through System.out, we do so through the constructor PrintStream fileoutput = new PrintStream(fileName); Can invoke any of the standard output methods we are familiar with print, println, printf Of course, as with file input, we can also create another instance of the PrintStream class to use for output to the screen

File Output (2): Writing Data to a File (cont.) An example of writing the numbers from 1-10 to a file, each on a separate line on its own Assume the file is called test.txt PrintStream fileoutput = new PrintStream( test.txt ); for(int index = 1; index <= 10; index++) { fileoutput.println(index); } File Output (3): Writing Data to a File (cont.) Some issues to consider when writing data to a file What if the file we specify does not exist? What if the file does exist and contains data in it will any existing data be lost? Will we simply append to it? Lets look at some examples to investigate these issue further I/O Streams

Introduction (1): What is a Stream? Think of it as a conduit Allows data to flow from the data origin point (e.g., keyboard, file etc.) to the point in our program where we access it With respect to the keyboard, Java contains an object of type java.io.inputstream Connected to the keyboard and ready to be used Keyboard plays the role of data source Program plays the role of data sink Input (1): Keyboard Input We can read from the keyboard as follow InputStream keyboard = System.out int input = keyboard.read(); API of read method indicates the method blocks until the user enters input User input is done when user presses ENTER Above method will return the character codes of the user input byte by byte each call to the read() method will return the next byte and will return -1 if called after last byte already read Input (2): Keyboard Input (cont.) But we haven t been reading from the keyboard using one byte at a time??? That would be very inconvenient would have to read each byte, convert to char and then somehow convert these chars to a int or float We delegate the work Reader conversion of byte stream to char stream Buffer collects chars until full line is accumulated and delivered to us when we asked

Input (3): Keyboard Input (cont.) Graphical illustration of the reader and buffer Input (4): Keyboard Input (cont.) Can employ delegation even further Rather than our application handling exchange of data between the reader and the buffer, why not have the reader report directly to the buffer? all we have to do then is deal with the buffer! Input (5): Keyboard Input (cont.) We can then create the needed instances bottomup and pass them to the constructor of the next aggregate in the chain InputStream keyboard = System.in; InputStreamReader reader = new InputStreamReader(keyboard); BufferedReader buffer = new Buffer(reader); We can now read input easily one line at a time by accessing the buffer only String input = buffer.readline()

Input (6): File Input (cont.) Very similar to keyboard input Still need to convert bytes to characters and to buffer the data except we now need an additional stream that connects to the file the FileInputStream does this for us Output (1): Screen Output We can write to the screen by establishing a data connection that begins in our application and ends at the screen This is an output stream since data moves away from our program As we know, Java comes with object of type java.io.printstream that is already connected to the screen leaves a reference of this as a field (called out) in the System class Output (2): File Output To write data to a file, we must delegate the aggregation shown below PrintWriter Accepts data of any primitive type or String and converts it to a character stream BufferedWriter Write text to a characteroutput stream, buffering characters

Output (3): File Output (cont.) OutputStreamWriter a bridge from character streams to byte streams FileOutputStream takes bytes from a stream and writes them to a file Such an approach does work but is inefficient Overhead incurred by the writer is expanded per character better to ask the writer to convert a whole string of characters at a time rather than one character at a time Output (4): File Output (cont.) Overhead incurred by the writer is expanded per character better to ask the writer to convert a whole string of characters at a time rather than one character at a time Buffer before the Writer As an Aside (1): Closing Streams All streams are auto-closed when a program ends without a run-time error Can also explicitly close a stream using the close() method good idea to do so! Closing an output stream will not only free resources but it will also flush the buffer Write any data that may still be in the buffer to the file Can also invoke the flush() method explicitly

As an Aside (2): Examples of File I/O Lets write and examine some code examples related to file I/O