The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 12 File Input and Output Animated Version.
Chapter 19 Binary I/O.
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.
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.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Lecture 13: File I/O Copyright © 2008 Xiaoyan Li.
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.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
© The McGraw-Hill Companies, 2006 Working with files Chapter 20.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Use JFileChooser F Text Input.
1 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,
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
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.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text 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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Two Ways to Store Data in a File  Text format  Binary format.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Files in Java 1.
1 Chapter 16 Simple Input and Output. 2 Objectives F To discover file properties, delete and rename files using the File class (§16.2). F To understand.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
Up to slide 46 for 111. Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
The Java IO System Different kinds of IO Different kinds of operations
IO in java.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
IO in java.
Chapter 17 Input and Output
Ch14 Files and Streams OBJECTIVES
Chapter 17 Binary I/O.
Object Writing in files
I/O Basics.
Chapter 17 Binary I/O 1.
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Streams and File I/O Chapter 9 Chapter 9.
Comp 249 Programming Methodology
Chapter 16 Simple Input and Output
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
The Package Statement Group related interfaces and classes together
David Davenport Spring 2005
Presentation transcript:

The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible outside the package –Classes in different packages can have the same names Creating a package class file –The first statement: package ; Where do we store a package’s class files? –In sub-folders of the parent package folder –Name the sub-folder: –Alternatives Create jar file and add to classpath environment variable Put into the lib/ext folder of JVM Package and sub packages only related by where they are stored

The Import Statement Example: import java.awt.event.*; Java automatically imports the package, java.lang The import statement allows references to package classes that are not fully qualified. It doesn’t actually load anything Sub packages are referenced using dot notation. Example: java.awt.Button. Button is a sub- package of java.awt Fully qualified names are always needed if the same class name appears in two or more packages.

Notes on the Import Statement Import can reference either a single package class or all classes of a package. Wildcard references do not apply to sub-packages. Import java.awt.Button allows us to WRITE b = new Button(“Clear”); instead of b = new java.awt.Button(“Clear”); Legal uses of import: a.import java.awt.*; okay b.import java.awt.E* no good. c.import java.awt.*; does not import java.awt.Event.*

Input and Output Streams Streams handle Java input and output. File transfer moves data to/from disk rather than to/from keyboard or screen

File I/O Concepts Files allow programs to access persistent data. Key terms: database, file, record, and field File access operations: open, read, write, close. Handle exceptions when dealing with files. –FileNotFoundException, IOException, EOFException File types are: Sequential and Random Data types are: Text, Binary, and Object File class: File file = new File(“pathname”) if (!file.exists())  file doesn’t exist.

Sequential Text File Streams Stream: Flow of source to sink Operations: sequential reads and writes of Strings BufferedReader in = new BufferedReader( new FileReader(“path”)); strVar = in.readLine(); in.close(); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(“path”))); out.println(“data”); out.close(); BufferedReader wraps FileReader –Reads entire line instead of a character at a time –The readLine() method returns null if at the end of file. PrintWriter contains print and println methods.

Binary Files Streams Operations: Reads and Writes of binary or text data DataOutputStream out=new DataOutputStream(new FileOutputStream(“path”)); DataInputStream in=new DataInputStream(new FileInputStream(“path”)); out.writeUTF(“data”);str = in.readUTF(); out.writeInt(x);int var = in.readInt(); out.writeDouble(3.5);double val = in.readDouble(); out.flush();in.close(); out.close(); 1.UTF stands for Unicode Text Format for writing and reading strings 2.DataOutputStream wraps FileOutputStrea to provide more methods for different data types.

Random File Read and Write RandomAccessFile raf = new RandomAccessFile(“path”,”access”); Read(byte[]), readBoolean(), readChar(), readDouble(), readFloat(), readInt(), readLine(), readLong(), readShort(), readUTF(), length(), setLength(), getFilePosition(), writeBoolean(), writeChar(), writeDouble(), writeFloat(), writeInt(), writeLine(), writeLong(), writeShort(), writeUTF(),seek(long), close(). “access” notes –Can be “rw”, “r”, “w”. –If “r”, the file must pre-exist. –If “rw” or “w” and the file doesn’t exist, it will be created. –“rwd” writes with immediate updates to storage. Seek first and then use the various methods

Writing objects to files Make the object serializable –Add Implements java.io.Serializable onto class signature line of any object that can be written to disk. –Place the keyword transient on any instance variables that are not to be serialized (ex: private transient int x;); Which streams? –ObjectOutputStream stream = new ObjectOutputStream( new BufferedOutputStream(newFileOutputStream(name))); –ObjectInputStream stream = new ObjectInputStream( new BufferedInputStream(new FileInputStream(name))); Which methods? stream.writeObject(objVariable); objVariable = (ObjectClassName)stream.readObject(); stream.close();

Check if File is Readable Boolean isReadable(String fileName) { File file = new File(fileName); if (!file.exists()) throw new FileNotFoundException(); if (!file.canRead()) throw new IOException(); }

Dialog for Choosing a File String str = System.getProperty ("user.dir"); JFileChooser chooser = new JFileChooser(str); int result = chooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); String fileName = file.getName(); System.out.println("You selected " + fileName); // Insert code here to open and access data from file } else System.out.println("You cancelled the file dialog");