File I/O ICS 111: Introduction to Computer Science I

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

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.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Yoshi
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
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.
FIT FIT1002 Computer Programming Unit 19 File I/O and Exceptions.
Lecture 7 File I/O (and a little bit about exceptions)‏
Files and Streams CS 21a Chapter 11 of Horstmann.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Files and Streams CS 21a. 10/02/05 L18: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
CS0007: Introduction to Computer Programming File IO and Recursion.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
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.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Introduction to Programming
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.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
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.
For Friday Finish reading chapter 9 WebCT quiz 17.
OO Design and Programming II I/O: Reading and Writing
CMSC 202 Text File I/O.
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Chapter 12 Exception Handling And Text IO
Streams and File I/O.
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Guide To UNIX Using Linux Third Edition
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Creating and Modifying Text part 2
Testing and Exceptions
CS Week 10 Jim Williams, PhD.
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
CSS161: Fundamentals of Computing
What/how do we care about a program?
Advanced Java Programming
String Input ICS 111: Introduction to Computer Science I
String Output ICS 111: Introduction to Computer Science I
CSS 161: Fundamentals of Computing
Command Line Arguments
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
CS 200 Command-Line Arguments & Exceptions
Web Design & Development Lecture 7
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Tutorial MutliThreading.
Java Basics Exception Handling.
Observer pattern, MVC, IO & 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:

File I/O ICS 111: Introduction to Computer Science I William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "The wireless music box has no imaginable commercial value. Who would pay for a message sent to nobody in particular?" David Sarnoff's associates in response to his urgings for investment in the radio in the 1920s. 12/1/2018 © 2007 William Albritton

Java Program Implementation Implement on UH UNIX using the command line interface The command line is the line at the prompt on the UNIX shell Commands Text editor: emacs Program.java Compiler: javac Program.java Interpreter: java Program A demo will be given in class 12/1/2018 © 2007 William Albritton

Command line Arguments Arguments (strings) entered on the command line can be accessed in your Java program args is an array of Strings public static void main(String [] args) Corresponds to the words listed after the name of the Java program Below is I/O for program Repeat.java java Repeat one two three args[0] is the String "one" args[1] is the String "two" args[2] is the String "three" 12/1/2018 © 2007 William Albritton

Command line & IDEs See file Repeat.java UHUNIX commands javac Repeat.java java Repeat one two three jGRASP commands Click Build, Run Arguments Run Arguments: one two three Eclipse commands Click Run, Run..., Arguments, Apply, Run Program Arguments: one two three 12/1/2018 © 2007 William Albritton

Command line & jGRASP You can also enter command line arguments using jGRASP Directions From the top menu, select “Build” On the drop-down menu, click “Run Arguments” At the top of the program, you should now have a text box titled “Run Arguments:” Type your command-line arguments into this text box 12/1/2018 © 2007 William Albritton

Meaning of main Meaning of each word of the main method public static void main(String[] args) public: other classes can use this method static: this is a class method, so method call has format ClassName.methodName() void: this method does not return anything main: name of this special kind of method which is used to start the program String[]: an array of Strings args: the parameter of this method, which contains each word entered on the command line, stored as an array of Strings 12/1/2018 © 2007 William Albritton

Class Exercise Write a Java program that creates a new word by combining the first letter of each word in the command line arguments The main method’s parameters are an Array of Strings (the command line arguments) & the main method displays a String (the new word) 12/1/2018 © 2007 William Albritton

Types of Exceptions Like other classes, exceptions have a class hierarchy See Java API & start at class Exception Can be broken down into two main types Runtime (unchecked) exceptions Checked exceptions 12/1/2018 © 2007 William Albritton

Types of Exceptions Runtime (unchecked) exceptions A type of exception that does not require a try-catch block Usually can be prevented by careful programming Not considered as a serious error Subclasses of class java.lang.RuntimeException, such as ArithmeticException, NullPointerException, IndexOutOfBoundsException, etc. 12/1/2018 © 2007 William Albritton

Types of Exceptions Checked exceptions A type of exception that must be placed in a try-catch block Signify that a “serious problem” has occurred Subclasses of class java.lang.Exception, such as IOException & FileNotFoundException 12/1/2018 © 2007 William Albritton

Files What is a file? Have two main types of files A collection of data with a name Allows for long-term storage Have two main types of files Binary and text files 12/1/2018 © 2007 William Albritton

Two Types of Files Text file A file that contains characters Words, numbers, symbols, etc. For example, the source files (*.java) of your programs are text files, as are *.html files A *.txt file is a text file Usually, 8-bits are used to store each character Each character corresponds to a particular numeric code called an “ASCII” code (American Standard Code for Information Interchange) Modern character standard now uses Unicode, which is used to encode all the languages in the world 12/1/2018 © 2007 William Albritton

Two Types of Files Binary file A file that may include text, but also includes some symbols that cannot be represented as text Usually must be interpreted by another program For example, compiled Java class files (*.class) are binary files Photographs (*.jpg or *.gif) are stored as binary files Microsoft Word files (*.doc) are stored as binary files, as are PowerPoint files (*.ppt) 12/1/2018 © 2007 William Albritton

Writing to a File Need import statements at top of program import java.util.Scanner; import java.io.PrintWriter; import java.io.FileNotFoundException; PrintWriter constructor needs a String The name of the file, if in current directory "myFile.txt" Or full name of path to the file "C:\ics111\progs\myFile.txt" Note that we are using the 1st command-line argument for the file name (args[0]) 12/1/2018 © 2007 William Albritton

Writing to a File PrintWriter constructor either creates a new file if it does not exist, or overwrites an existing file Must have a try-catch block, as will throw a checked exception if path name cannot be found String fileName = "myFile.txt"; PrintWriter writer = null; try { writer = new PrintWriter(fileName); } catch(FileNotFoundException e){ System.out.println(e); } 12/1/2018 © 2007 William Albritton

Writing to a File Example program See WriteToFile.java Writes to a file, which has the same name as the 1st command-line argument (args[0]) If no arguments are entered, then the program ends before writing to a file Note that out of System.out is of class PrintStream, which has many methods which are similar to PrintWriter, such as print() and println() For file names, use fileName.txt, so it is easy to view the files 12/1/2018 © 2007 William Albritton

Reading from a File Need import statements at top of program import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; File constructor needs a string that is either The name of the file if in current directory "myFile.txt" Or full name of path to the file "C:\ics111\prog\myFile.txt" This is does not create a file on disk It only connects our program to a file 12/1/2018 © 2007 William Albritton

Reading from a File Use File object in Scanner’s constructor Must have a try-catch block, as will throw a checked exception if file cannot be found String fileName = "myFile.txt"; File file = new File(fileName); Scanner scanner = null; try{ scanner = new Scanner(file); } catch(FileNotFoundException e){ System.out.println(e); } 12/1/2018 © 2007 William Albritton

Reading from a File Example code See ReadFromFile.java Reads from a file, which has the same name as the 1st command-line argument (args[0]) If file does not exists, with throw FileNotFoundException and end program Scanner method hasNextLine() will return “true” if more lines are in the file, otherwise “false” 12/1/2018 © 2007 William Albritton