CS Week 11. Jim Williams, PhD

Similar documents
CS 200 File Input and Output Jim Williams, PhD

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

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

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

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Darshan Institute of Engineering & Technology for Diploma Studies

Object-Oriented Programming Design. Topic : Streams and Files

IT101. File Input and Output

Recitation: Loop Jul 7, 2008

CS Week 5. Jim Williams, PhD

Here is a hierarchy of classes to deal with Input and Output streams.

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CS Programming I: File Input / Output

CSC 1214: Object-Oriented Programming

CS Programming I: File Input / Output

Input-Output and Exception Handling

CS 1301 Ch 8, Handout 3

Dining philosophers (cont)

CS11 Java. Fall Lecture 4

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS 251 Intermediate Programming Java I/O Streams

Chapter 10. IO Streams

PIC 20A Streams and I/O

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Here is a hierarchy of classes to deal with Input and Output streams.

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

File Operations in Java. File handling in java enables to read data from and write data to files

CS Week 14. Jim Williams, PhD

Software Practice 1 - File I/O

JAVA - FILE CLASS. The File object represents the actual file/directory on the disk. Below given is the list of constructors to create a File object

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

import java.io.*; class OutputExample { public static void main(string[] args) { try{ PrintWriter pw = new PrintWriter

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

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

WOSO Source Code (Java)

CS Introduction to Data Structures Tuesday, February 2, 2016

Simple Java Input/Output

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

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

Input from Files. Buffered Reader

CSPP : Introduction to Object-Oriented Programming

1.00 Lecture 30. Sending information to a Java program

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

Week 6 CS 302. Jim Williams, PhD

Exception Handling CSCI 201 Principles of Software Development

TEXT-BASED APPLICATIONS

I/O Streams. Object-oriented programming

Networking Code CSCI 201 Principles of Software Development

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Sri Vidya College of Engineering & Technology Question Bank

Exceptions and Working with Files

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

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

Chapter 10 Input Output Streams

Special error return Constructors do not have a return value What if method uses the full range of the return type?

CS 302 Week 9. Jim Williams

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

Defensive Programming. Ric Glassey

Exceptions and Libraries

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

HST 952. Computing for Biomedical Scientists Lecture 8

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

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

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

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

CN208 Introduction to Computer Programming

Exceptions - Example. Exceptions - Example

Welcome to the Using Objects lab!

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

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Data Structures. 03 Streams & File I/O

1. An operation in which an overall value is computed incrementally, often using a loop.

CS 200 Using Objects. Jim Williams, PhD

Exception Handling. CSE 114, Computer Science 1 Stony Brook University

FILE I/O IN JAVA. Prof. Chris Jermaine

Excep&ons and file I/O

Programming Languages and Techniques (CIS120)

Java Input/Output. 11 April 2013 OSU CSE 1

Exceptions Handling Errors using Exceptions

Object-oriented programming in Java (3)

CS Week 2. Jim Williams, PhD

I/O STREAM (REQUIRED IN THE FINAL)

Project #1 Computer Science 2334 Fall 2008

CS163/164 Final Exam Study Session

Unit 10: exception handling and file I/O

Give one example where you might wish to use a three dimensional array

Streams and File I/O

Classes Basic Overview

CS159. Nathan Sprague

CS 201, Fall 2016 Sep 28th Exam 1

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

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

Lab 11. A sample of the class is:

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

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

EXCEPTIONS. Fundamentals of Computer Science I

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

A sample print out is: is is -11 key entered was: w

Transcription:

CS 200 - Week 11 Jim Williams, PhD

This Week 1. Exam 2 - Thursday 2. Team Lab: Exceptions, Paths, Command Line 3. Review: Muddiest Point 4. Lecture: File Input and Output

Objectives 1. Describe a text file including meta information and contents. 2. Trace, explain and write programs that read the contents of a text file. 3. Trace, explain and write code that writes a text file.

Relative vs Absolute Paths Relative - based on current location Absolute - based on fixed location

From within log what is a relative path to tasks? / meta log mylyn tasklistindex segments tasks contexts plugins history../mylyn/tasks /meta/mylyn/tasks./mylyn/tasks../tasks

From within tasks what is a relative path to mylyn? / meta log mylyn tasklistindex segments tasks contexts plugins history /meta/mylyn../mylyn./../mylyn/tasks..

From within tasks what is an absolute path to log? / meta log mylyn tasklistindex segments tasks contexts plugins history /meta/log../../log./meta/log /log

What is the correct output? public static void main(string[] args) { String name = "Fred"; int num = 100; double dnum = 3.14159; System.out.printf("Hello %6s\n", name); System.out.printf("%05d,%.2f", num, dnum); http://docs.oracle.com/javase/7/docs/api/java/util/formatter.html#syntax Hello Fred 100,3.14 Hello Fred 100,3.14 Hello Fred 100,3.14159 Hello Fred 00100,3.14

File Input and Output Key Classes: File - meta information Scanner, InputStream, FileInputStream, FileReader, BufferedReader OutputStream, FileOutputStream, PrintStream, PrintWriter

File Use to access file meta information File file = new File("src/CheckFile.java"); exists(), isdirectory(), isdirectory(), getname(), getpath(), canread(), canwrite(), etc.

What does this do? public static void show(file file, String depth) { File[] files = file.listfiles(); for (int i = 0; i < files.length; i++) { System.out.printf("%s%s\n", depth, files[i].getname()); if (files[i].isdirectory()) show(files[i], depth + " "); public static void main(string[] args) { File file = new File("."); show(file, " ");

File Input

What is print out? File f = new File( "myfile.txt"); Scanner scnr = new Scanner( f); scnr.nextline(); int count = 4; if ( scnr.hasnextint()) count = scnr.nextint(); else { scnr.nextline(); count = scnr.nextint(); for ( int i = 0; i < count; i++) scnr.nextline(); System.out.println( scnr.nextline()); 3 hello 2 8 line 5 line 3 line 4 line 8 line 5 line 3 line other

What does this do? File file = new File(FILENAME); Scanner infile = null; try { infile = new Scanner(file); while (infile.hasnext()) { //process each line of the file catch (FileNotFoundException e) { e.printstacktrace(); finally { if (infile!= null) infile.close();

Try with Resources //Prior to Java 7 BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readline(); finally { if (br!= null) br.close(); //In Java 7 try ( BufferedReader br = new BufferedReader(new FileReader(path)) ) { return br.readline();

What does this do? StringBuilder sb = new StringBuilder(); try ( //try with resources, calls close when done Scanner input = new Scanner( sourcefile); ) { while ( input.hasnext() ) { String s1 = input.nextline(); String s2 = s1.replaceall( args[1], args[2]); sb.append(s2 + "\n"); https://docs.oracle.com/javase/tutorial/essential/exceptions/tryresourceclose.html

If methoda throws GreenException then output is? public static void main(string[] args) throws GreenException { boolean logging = false; try { methoda(); System.out.print("1"); catch( BlueException e) { System.out.print("2"); catch( GreenException e) { if ( logging) System.out.print("3"); throw e; System.out.print("4"); 234 4 34 stack trace

Can you catch unchecked exceptions? yes, all throwables no, only checked exceptions

Who/What is 'checking' the checked exceptions? programmer user compiler virtual machine (JVM

What will print out? public static void change( char [][] map) { map[1][1] = 'A'; public static void main( String []args ) { char [][] map = new char[5][4]; map[1][1] = 'B'; change( map ); System.out.println( map[1][1]); A B wish I knew other

What will print out? public static void change( char [][] address ) { address [1][1] = 'A'; public static void main( String []args ) { char [][] address = new char[5][4]; address [1][1] = 'B'; change( address ); System.out.println( address [1][1]); A B wish I knew other

Review Arrays and Methods public static void change( char [][] map) { map[1][1] = 'A'; public static void main( String []args ) { char [][] map; map = new char[5][4]; change( map ); System.out.println( map[1][1]);

How many 'map' variables are there? public static void change( char [][] map) { map[1][1] = 'A'; public static void main( String []args ) { char [][] map; map = new char[5][4]; change( map ); System.out.println( map[1][1]); 1 2 3 other

Is main 'map' on stack or heap? public static void change( String[][] map) { map[1][1] = "A"; public static void main( String []args ) { String [][] map = new String[5][4]; map[1][1] = "B"; change( map ); System.out.println( map[1][1]); stack heap other

What is data type of map[1]? public static void change( char [][] map) { map[1][1] = 'A'; public static void main( String []args ) { char [][] map; map = new char[5][4]; change( map ); System.out.println( map[1][1]); char char [] char [][] other

Read File - Application Some Data Sets https://github.com/fivethirtyeight/data/find/8eb18e0b6115ae6fcfb49bf81ccdd01cbe 37906c http://rs.io/100-interesting-data-sets-for-statistics/ http://www.gutenberg.org/ebooks/29765 https://github.com/dwyl/english-words

File Output

What does this do? String name = "myfile.txt"; File afile = new File(name); PrintWriter output = new PrintWriter( afile); output.printf("this is the contents of %s.\n", name); output.close();

What does this do? PrintWriter writer = null; try { writer = new PrintWriter( SEEDS_FILENAME); for ( Seed s : seeds) { writer.printf("%f, %f\n", s.getx(), s.gety()); catch ( IOException e) { System.err.println("Unable to write to " + SEEDS_FILENAME); finally { if ( writer!= null) writer.close();