Accessing Files in Java

Slides:



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

Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
Lecture 7 File I/O (and a little bit about exceptions)‏
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
A Few Exceptions, A Little IO, and Persistent Objects Rick Mercer.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 15 – Files I/O basics File I/O Classes File I/O Basic Operations Text File Output PrintWriter import Statement with a * Text File Input Scanner,
Java I/O Writing and Reading Objects to File Serialization.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
Object Persistence and Object serialization CSNB534 Asma Shakil.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
Two Ways to Store Data in a File  Text format  Binary format.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
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.
Java IO Exploring the java.io package and living to talk about it.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Networking Serialization
I/O Basics.
Computer Programming Methodology File Input
Testing and Exceptions
Revision.
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Programming in Java Files and I/O Streams
CS 116 Object Oriented Programming II
Chapter 12 File Input and Output
Exceptions Complicate Code
Exceptions Complicate Code
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Files Extra Example.
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Exceptions Complicate Code
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Exceptions.
Exceptions.
Podcast Ch23c Title: Binary Files
Presentation transcript:

Accessing Files in Java Lesson 5

Reading Text from a File Use FileInputStream and DataInputStream File types are “wrapped” FileInputStream in = new FileInputStream ("nameNumber.txt"); DataInputStream s = new DataInputStream (in); Command then used is stringVal = s.readLine() Note Source code of readStrings.java

Program Features Note the specification of exception handling public static void main(String args[ ]) throws IOException This requires a try block and a catch block catch (IOException ioe) { } Note contents of input file and resulting output

Declaring a Class Declared in a separate file – nameRec.java Methods declared, implemented within the class declaration Note use of public specifiers (some things can also be private) For objects of this class to be saved as binary file records, implements Serializable must be declared Class will be used by another program

Must specify this for saving objects of this type to a file Declaring a Class public class nameRec extends Object implements Serializable{   public String name; public double dNum; public int iNum; public void nameRec() { name = ""; dNum=0.0; iNum = 0; } public String getName() { return name; } Must specify this for saving objects of this type to a file Data attributes Constructor

Declaring a Class public void setRec (String n, double d, int i) { name = n; dNum = d; iNum = i; } public void nameRec(String n, double d, int i) setRec (n, d, i);   public void printRec () System.out.println ("Record : "+name+" "+dNum+" "+iNum); Another constructor

Creating a Binary Data File ReadNameNum.java nameRec object instantiated Open the text file to be read FileInputStream wrapped in DataInputStream Open the file for nameRec object output FileOutputStream wrapped in ObjectOutputStream

Creating a Binary Data File Prime the pump Check for end of file Process the record Cast object as it is written Try to read the next record nRec = readRecord (in); while (nRec.getName() != null) { nRec.printRec(); s.writeObject ((nameRec) nRec); nRec = readRecord(in); }

Manipulating Text to Load the Object public static nameRec readRecord (DataInputStream in) throws IOException { String name = null, iString = null, dString = null; name = in.readLine(); iString = in.readLine(); dString = in.readLine() ; nameRec temp = new nameRec (); if (dString == null) return temp; // no more records Double d = Double.valueOf(dString); Integer i = Integer.valueOf(iString); int ii = i.intValue(); double dd = d.doubleValue();   temp.setRec (name, dd,ii); return temp; } Reading the data Change strings to Integer and Double “wrapped” objects Call nameRec set method

Reading Objects from a File readNameRec.java Note import nameRec; The wrapping of the FileInputStream in the ObjectInputStream file Instantiation of the nameRec object throw, try, and catch specifications

Reading Objects from a File public class readNameRec { public static void main (String[] args) throws IOException, ClassNotFoundException { ObjectInputStream s = (new ObjectInputStream (new FileInputStream ("nameNum.dat"))); nameRec nRec = new nameRec(); try { while ((nRec = (nameRec)s.readObject()) != null) nRec.printRec(); } catch (IOException ioe) { } catch (ClassNotFoundException cnfe) { } Error handling specifications Do the I/O in the condition

Results of the File Read Line printed by the printRec method