David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.

Slides:



Advertisements
Similar presentations
Taking Input Java Md. Eftakhairul Islam
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
CS0007: Introduction to Computer Programming File IO and Recursion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
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.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
CS 46B: Introduction to Data Structures June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 9 1 Chapter 9 – Part 2 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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
 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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Programming – Lecture 10 Files, Exception handling (Chapter 12.4)
CMSC 202 Text File I/O.
Introduction to programming in java
Five-Minute Review How do we construct a GPolygon object?
File handling and Scanning COMP T1
Objectives You should be able to describe: Interactive Keyboard Input
University of Central Florida COP 3330 Object Oriented Programming
Building Java Programs
Strings and File I/O.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Five-Minute Review How do we construct a GPolygon object?
Unit 6 Working with files. Unit 6 Working with files.
18 File i/o, Parsing.
Input/output (I/O) import java.io.*;
Introduction to Computing Using Java
Input/output (I/O) import java.io.*;
Input/output (I/O) import java.io.*;
Input/output (I/O) import java.io.*;
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Five-Minute Review How do we construct a GPolygon object?
Presentation transcript:

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning COMP T1 #6

COMP 112 6: 2 Overview Reading and writing from files Paths Try {..} catch {..} for risky code. Scanners

COMP 112 6: 3 Introduction Java file handling uses many Classes including: 1.File FileReader FileWriter 2.Path Look at the API to find specialised file handling. Scanners transform a string into a sequence of tokens. Frequently the contents of asci files are read using a scanner. Scanners implement the interface Iterator

COMP 112 6: 4 Files Java will find files in the directory from which the program is run just by using the file name. Java IO design is based on the Decorator pattern This can lead to unhelpfully complex libraries! Beware java NIO (nonblocking) exists as well My advice is keep it simple and understand the basics 1. File internal representation of a file 2. FileReader for reading one byte at a time 3. BufferedReader for reading one line at a time 4. Scanner for reading one word (token) at a time Best to understand simple Example Code:

COMP 112 6: 5 Files Examples of common idioms: new Scanner(new File("README.TXT"))); new BufferedReader(new FileReader("README.TXT"))); new File("README.TXT") dose not mean that the file README.TXT is new (just created). It means that a new object in the java program is created linking to a potentially existing README.TXT file. new Scanner(new File("README.TXT"))); creates a new object that will scan (read and output tokens) from the README.TXT file.

COMP 112 6: 6 Risky code Any thing that is risk (opening a file that might not exist) should be performed in a try block try { …} and you need to catch these errors catch { …} and process them. Even if only by printing out an error message. Uncaught errors will crash the program!

COMP 112 6: 7 Example From “ README.TXT ” we build a File then a Scanner. Build a Scanner Failing can throw exceptions File opening can fail Catch IOexceptions from the try block Open a file

COMP 112 6: 8 Paths and Files Files are held in a rooted directory tree structure. A Complete Path has the names from the root down the tree to the file. /home/users/dstr/Comp112/Lectures/foo.ppt A Relative Path is a Path from the current location to a file. If the program is run in my home directory /home/users/dstr the files relative path is Comp112/Lectures/foo.ppt Note absolute paths start with a / but relative paths do not. Unix and Microsoft computers use paths with different separators. Unix uses / but Microsoft uses \. Java can use File.separator and the JVM will automatically adjust depending on the operating system

COMP 112 6: 9 File Choosing When you don not know the name of the file but want to browse the directories use, s = new Scanner(new File(UIFileChooser.open())); in place of, s =new Scanner(new File("README.TXT")));

COMP 112 6: 10 Scanner Class The Scanner class provides all the text input methods from the UI class. But can take input from Files s = new Scanner(new File(“README.TXT”)); The following methods you should recognize hasNext, hasNextInt, hasNextBoolean, hasNextDouble next, nextInt, nextBoolean, nextDouble, nextLine Scanner can take as input: 1. File 2. Path 3. String

COMP 112 6: 11 Delimiter Scanner default delimiter is white space that is: the sequence of token the scanner reads are separated by white space. To change the delimitor use s.useDelimiter(",\\s*");

COMP 112 6: 12 Exercises: Read a file where each line can have different number of tokens (words). Output the sequence of tokens and special token to mark where the lines end. Read a file with n integers on each of m lines and output an n*m array of integers.

COMP 112 6: 13 Summary Classes: File, Path, Scanner, UIFileChooser 1.Look at the API 2.Look at code examples Decorator pattern – wrapping one class around another - is a common idiom but has known problems