Peer Instruction 9 File Input/Output.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
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.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
18 File handling1June File handling CE : Fundamental Programming Techniques.
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.
11 Chapter 4 LOOPS AND FILES CONT’D. 22 SENTINEL-CONTROLLED LOOPS Suppose we did not know the number of grades that were to be entered. Maybe, a single.
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.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Input/Output and Exception Handling.
Two Ways to Store Data in a File Text format Binary format.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Program 6 Any questions?. System.in Does the opposite of System.out.
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.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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.
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.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
File - CIS 1068 Program Design and Abstraction
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
CS 160 Final Review.
Reading from a file and Writing to a file
Input/Output.
Streams and File I/O.
Strings and File I/O.
File Input and Output TOPICS File Input Exception Handling File Output.
Week 14 - Wednesday CS 121.
I/O Basics.
Haidong Xue Summer 2011, at GSU
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
Methods and Parameters
File Input and Output TOPICS File Input Exception Handling File Output.
INPUT STATEMENTS GC 201.
CSS161: Fundamentals of Computing
Peer Instruction 6 Java Arrays.
Advanced Java Programming
Streams and File I/O Chapter 14.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
CSS 161: Fundamentals of Computing
A+ Computer Science INPUT.
Unit 6 Working with files. Unit 6 Working with files.
A+ Computer Science INPUT.
Java Exceptions Dan Fleck CS211.
Peer Instruction 4 Control Loops.
I/O Exceptions & Working with Files
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.
Presentation transcript:

Peer Instruction 9 File Input/Output

cs163/164: Peer 9 - File Input/Output - Fall Semester 2016 Which line of code creates a Scanner to read a file named by the String inputFile? Scanner in = new Scanner(inputFile); Scanner in = new Scanner("inputFile"); Scanner in = new Scanner (System.in); Scanner in = new Scanner(new File(inputFile)); Scanner in = new Scanner(new File("inputFile")); makes a Scanner for the filename. makes a Scanner for the literal. makes a Scanner for the keyboard. D) correct answer, makes a File object using the file name. E) only works if the file name is “inputFile” File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

cs163/164: Peer 9 - File Input/Output - Fall Semester 2016 Which line of code creates a PrintWriter to write a file named "output.txt"? PrintWriter out = new PrintWriter (output.txt); PrintWriter out = new PrintWriter ("output.txt"); PrintWriter out = new PrintWriter (System.out); PrintWriter out = new PrintWriter (new File(output.txt)); PrintWriter out = new PrintWriter (new File("output.txt")); Answer is B) or E), need a File object and literal was specified, overloaded constructor! File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

cs163/164: Peer 9 - File Input/Output - Fall Semester 2016 Given the file contents in red and a Scanner in, which values are read to d0/d1/d2? double d0 = in.nextDouble(); double d1 = in.nextDouble(); double d2 = in.nextDouble(); File contents: 11.1 83 22.2 33.3 1234 11.1, 22.2, 33.3 11, 83, 22 11.1, 83.0, 22.2 11.1, exception! Answer is C), integers in the input will be promoted to doubles. File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

cs163/164: Peer 9 - File Input/Output - Fall Semester 2016 Given the code shown, how many lines in the output file? Assume out is a PrintWriter. out.print("123\thello\n"); out.println("there\t&&\n"); out.println("\ngoodbye"); 3 lines 4 lines 5 lines 6 lines 7 lines Answer is C), two println statements and three newlines. File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

Which of the following lines are incorrect in the code shown below? 1 File in = new File(inputFile); 2 try { 3 Scanner s = new Scanner(inputFile); 4 while (s.hasNextLine()) 5 System.out.println(s.next()); 6 } catch (IOException) { 7 s.close(); 8 } Line 3 Line 4 and 5 Line 6 Line 7 What a mess! Answer is E), line 3 has inputFile instead of in, 4 and 5 are dangerous, 6 is missing object, 7 has close in catch! File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

cs163/164: Peer 9 - File Input/Output - Fall Semester 2016 What are the values of the four variables after executing the code below? int i = keyboard.nextInt(); double d = keyboard.nextDouble(); String s0 = keyboard.nextLine(); String s1 = keyboard.next(); File contents: 12 5.4 Hello There 12, 5.4, "", "Hello" 12, 5.4, "Hello There", "" 12, 5.4, "", "Hello There" 12, 5.4, "\n", "Hello" 12, 5.4, "\n", "Hello\n" Answer is A), the nextLine() calls reads from end of previous token to end of line, discards newline. File Input cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

How many bytes are written to the file by the code shown below: PrintWriter out = null; try { out = new PrintWriter("out.txt"); out.println("Hello World!"); } catch (IOException e) { out.close(); } 11 12 13 No file written! Answer is A) or D), file is never closed. File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

How many bytes are written to the file by the code shown below: try { PrintWriter out = new PrintWriter(new File("out.txt")); out.println("Hello World!"); out.close(); } catch (IOException e) { // Error message } 11 12 13 No file written! Answer is D), length of string is 12 plus newline = 13 File Output cs163/164: Peer 9 - File Input/Output - Fall Semester 2016

Which definition of checked or unchecked exception is correct? Unchecked: the program must catch/throw the exception. Unchecked: the program cannot catch/throw the exception. Checked: the program must catch/throw the exception. Checked: the program cannot catch/throw the exception. None of the above Answer is C), unchecked means catch/throw is optional, checked means must catch/throw. A method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow. Java Exceptions cs163/164: Peer 9 - File Input/Output - Fall Semester 2016