Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Advertisements

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.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
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.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
CS 206 Introduction to Computer Science II 09 / 04 / 2008 Instructor: Michael Eckmann.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
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.
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.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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.
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.
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.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
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.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
For Friday Finish reading chapter 9 WebCT quiz 17.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Streams and File I/O.
University of Central Florida COP 3330 Object Oriented Programming
Program Input/Output (I/O)
Creating and Modifying Text part 2
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
Chapter 13: File Input and Output
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
CMPE212 – Stuff… Assn 2 due this Friday. Winter 2018
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Unit 6 Working with files. Unit 6 Working with files.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CMPE212 – Reminders Assignment 3 due next Friday.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
File Input and Output.
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks in the context of file I/O.) Continue File Input/Output (concentrate on text I/O) Next topic - Encapsulation Winter 2015CMPE212 - Prof. McLeod1

Back to File I/O I will not discuss streams, which are at the root of I/O. Java provides other classes to handle these streams and make their use easier. There are many ways to do file I/O. Sometimes experimentation is needed to find the most efficient technique. I will focus on techniques that are applicable to relatively small files. BTW, file I/O is slowwwwww.... Winter 2015CMPE212 - Prof. McLeod2

Aside - Buffered vs Non-Buffered I/O You can choose between: Non-Buffered: you interact directly with the device – your hard-disk for example. Every read and write takes place immediately. Slowest. Winter 2015CMPE212 - Prof. McLeod3

Buffered vs Non-Buffered I/O, Cont. Buffered: Reads and writes take place through a memory store in RAM – a buffer. For reading – re-reads are much faster. For writing – the buffer is emptied either when it is full or when you “flush” it. Winter 2015CMPE212 - Prof. McLeod4

Buffered vs Non-Buffered I/O Buffered I/O is much faster as it minimizes the physical file reads and writes. But, suppose you lose your physical connection to your file before the buffer is emptied? Data can be lost and/or corrupted. Winter 2015CMPE212 - Prof. McLeod5

Java 7 Path Object File I/O resources and methods work with Path objects, not directly with files. The new Java 7 file I/O objects are in the java.nio library. Path import is import java.nio.file.Path; for example. (You can also let Eclipse help you find imports...) Winter 2015CMPE212 - Prof. McLeod6

Java 7 Path Object, Cont. To create a Path object linked to a file invoke the static Paths.get() method: Path aFile = Paths.get(filename); filename is a String. filename can contain a path like: “C:\\Users\\Alan\\workspace\\projectDemo\\src\\Stuff.txt” If there is no path then Eclipse will assume the file is in the project folder – not the src folder. Winter 2015CMPE212 - Prof. McLeod7

Java 7 Path Object, Cont. Folder delimiters depend on your OS. Path objects can be created from files and folders that don’t exist! Path objects can be split up, compared and joined. Path objects are also supplied to static methods in the Files class. Winter 2015CMPE212 - Prof. McLeod8

The Files Class Main entry point to file operations. Contains static methods that: –Create objects for file input and output. –Are used for checking, deleting, copying, creating and watching files and directories. –Can view and change file metadata. Just about all operations will require, at a minimum, a Path object. Winter 2015CMPE212 - Prof. McLeod9

The Files Class, Cont. The Path object created may refer to a path or file that does not exist, so you had better check before you attempt to use it. For example: if (Files.exists(file, LinkOption.NOFOLLOW_LINKS) && Files.isReadable(file) ) {... The LinkOption thing tells the method what to do if the file is a symbolic link. Winter 2015CMPE212 - Prof. McLeod10

Text File Input, Cont. To count the lines in a text file, for example: Charset charset = Charset.forName("US-ASCII"); try (BufferedReader reader = Files.newBufferedReader(file, charset)) { do { line = reader.readLine(); lineCount++; } while (line != null); lineCount--; } catch (IOException err) { System.err.println("Unable to read file: " + filename); } Winter 2015CMPE212 - Prof. McLeod11

Easier Text File Input The Files class has a static method called.readAllLines() that reads and returns all lines of text as a List object, which can be cast to be of type ArrayList. See TextFileIOUtils.java The try with resources block is not always used even for new Java 7 code. Note use of static object generator methods. Winter 2015CMPE212 - Prof. McLeod12

Text File Output Again, consider buffered output only. As with input, you can create a BufferedWriter resource using Files.newBufferedWriter(). Then invoke the write() method to write a single String to the file. See the code snippet on the next slide from TextFileIOUtils.java: Winter 2015CMPE212 - Prof. McLeod13

Text File Output, Cont. try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) { for (int i = 0; i < text.length; i++) { writer.write(text[i] + "\r\n"); lineCount++; } } catch (IOException err) { System.err.format("IOException: %s%n", err); System.err.println("Unable to write file: " + filename); return lineCount; } Winter 2015CMPE212 - Prof. McLeod14

Text File Output, Cont. The.newBufferedWriter() method can also accept a third OpenOptions parameter. A few, common parameters (enums) are: –WRITE – Opens the file for write access. –APPEND – Appends the new data to the end of the file. This option is used with the WRITE or CREATE options. –TRUNCATE_EXISTING – Truncates the file to zero bytes. This option is used with the WRITE option. –CREATE_NEW – Creates a new file and throws an exception if the file already exists. –CREATE – Opens the file if it exists or creates a new file if it does not. –DELETE_ON_CLOSE – Deletes the file when the stream is closed. This option is useful for temporary files. Winter 2015CMPE212 - Prof. McLeod15

Text File Output, Cont. If you don’t supply a third parameter, the method will: –create a new file if it doesn’t exist, or –empty (truncate) an existing file – without warning. So, you might wish to check to see if the file exists and ask the user if he wishes to overwrite it. Winter 2015CMPE212 - Prof. McLeod16

Easier Text File Output The Files class also has a write() method that can write an entire ArrayList object, appending the system’s current line termination sequence to each String. This method must get an “iterable” object – it cannot work directly with a String[] object. Winter 2015CMPE212 - Prof. McLeod17

Text File I/O Comparisons Using TextFileIOUtils.java: Reading is a bit slower than writing. The new Java 7 reading is a bit faster than the Java 6 version. Java 7 vs Java 6 writing times are about the same – but buffering from the reading may affect these times. The effect of buffering can easily be demonstrated. The Java 7 code can be more compact and should be safer. Winter 2015CMPE212 - Prof. McLeod18