File I/O CLI: File Input CLI: File Output GUI: File Chooser

Slides:



Advertisements
Similar presentations
Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Advertisements

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.
FIT FIT1002 Computer Programming Unit 19 File I/O and Exceptions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Files and Streams CS 21a Chapter 11 of Horstmann.
18 File handling1June File handling CE : Fundamental Programming Techniques.
File I/O There’s more to life than the keyboard. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction.
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.
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.
Announcements Quiz 2 Grades Posted on blackboard.
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
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.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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.
Input and Output Using Text Files and Exception Handling.
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.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
BHCSI Programming Contests. Three contests Mock Contest #1  Friday, July 16 th  2:15 – 4:45pm UCF High School Online Contest  Thursday, July 22 nd.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
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.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
Introduction to programming in java
CSC 211 Java I File I/O and more methods. Today’s plan Homework discussion Reading lines from files Writing lines to files All of the above, using methods.
java.io supports console and file I/O
Key Words / Reserved Words
Part I General Principles
CSC111 Quick Revision.
File - CIS 1068 Program Design and Abstraction
File Input / Output.
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Introduction to programming in java
Reading from a file and Writing to a file
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Chapter 12 Exception Handling And Text IO
Section 64 – Manipulating Data Using Methods – Java Swing
Strings and File I/O.
Maha AlSaif Maryam AlQattan
File Input and Output TOPICS File Input Exception Handling File Output.
I/O Streams- Basics Byte Streams and Character Streams
File Input and Output TOPICS File Input Exception Handling File Output.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Introduction to Classes and Methods
September 9, 2008 Lecture 5 – More IO.
Introduction to Computing Using Java
September 9, 2008 Lecture 5 – More IO.
Introduction to Java Brief history of Java Sample Java Program
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
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:

File I/O CLI: File Input CLI: File Output GUI: File Chooser Reading for this lecture: L&L 9.8

CLI File Input In a CLI, we want the user to select a file within a directory system so that its contents can be read and processed However, we must rely on the user typing in the file name (including any required path name) We can get the file name via a Scanner on System.in using the nextLine method We can read the file data via a Scanner on a File object using the nextLine method again

CLI File Input: Example import java.util.Scanner; import java.io.*; public class FileDisplay { public static void main (String [] args) throws IOException Scanner scan = new Scanner(System.in); System.out.println("Enter name of file to display"); File file = new File(scan.nextLine()); scan = new Scanner (file); // done with keyboard while (scan.hasNext()) System.out.println(scan.nextLine()); }

CLI File Output In a CLI, we want the user to create a file within a directory system so that its contents can be written (or overwritten!) Be careful: Your code should check for a file by that name and ask user if OK to overwrite it. Again, we rely on the user typing in the file name Again, we can get the file name via a Scanner on System.in using the nextLine method We can write the file data via a PrintStream on a File object using the println method (System.out is a PrintStream object)

CLI File Output: Example import java.util.Scanner; import java.io.*; public class FileWrite { public static void main (String [] args) throws IOException // Get filename and instantiate File object as before PrintStream out = new PrintStream(file); while (scan.hasNext()) { String line = scan.nextLine(); if (line.equals("END")) // A sentinel String value break; else out.println(line); } out.close();

GUI File I/O In a GUI, requiring the user to enter a file name (including a path name or not) is considered to be NOT very user friendly We want our program to offer a choice of the available files so that the user can: Move around within the available directories Select one of the files shown in a directory

File Chooser in GUI’s Recall that a dialog box is a small window that "pops up" to interact with the user for a brief, specific purpose A file chooser, the JFileChooser class, supports a simple dialog box for this process See DisplayFile.java (page 516)

Example: DisplayFile code segment JFileChooser chooser = new JFileChooser(); int status = chooser.showOpenDialog(frame); // There is also a showSaveDialog(frame) if (status != JFileChooser.APPROVE_OPTION) ta.setText ("No File Chosen"); else { // read file File file = chooser.getSelectedFile(); Scanner scan = new Scanner (file); ...

File Input/Output Notice that the main method in all three of these examples indicates that the code may throw an IOException If an error such as “file not found” occurs during a file operation, an IOException is generated by the system We’ll study exceptions later