The Package Statement Group related interfaces and classes together

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.
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.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Files CSC 171 FALL 2001 LECTURE 18. History Operating Systems Operating systems (originally called monitors or supervisors) had been developed in the.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File input and output. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction with a user, who types in.
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 9 Streams and File I/O Overview of Streams and File I/O
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 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.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
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.
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.
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.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
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,
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Binary Files, Random Access Files Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 9 l Streams and Simple File I/O l Exception Handling with File.
Two Ways to Store Data in a File  Text format  Binary format.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
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
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.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
java.io supports console and file I/O
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
BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.
IO in java.
Chapter 17 Input and Output
Chapter 17 Binary I/O.
Object Writing in files
Strings and File I/O.
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
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.
Chapter 12 File Input and Output
Chapter 17 – Files and Streams
MSIS 670: Object-Oriented Software Engineering
Comp 249 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
Computer Programming with JAVA
Chapter 10 File I/O Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
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 <package name>; Where do we store a package’s class files? In sub-folders of the parent package folder Name the sub-folder: <package name>

Packages in eclipse Default location for source files is: projectFolder/src. You can make a package directory by r-clicking the project | new package: Notice the folder holding the sources for the new package is a subdirectory of the src directory.

The Import Statement Example: import java.awt.event.*; Import all the classes in the package java.awt.event If you use ‘*’, it must appear immediately following a ‘.’ Java automatically imports the package, java.lang Sub packages are referenced using dot notation. Example: java.awt.Button. Button is a sub-package of java.awt Example: Make new packages pkga and inside pkga, pkgb. Reference pkgb as pkga.pkgb (the fully qualified name of pkgb in java). Eclipse creates the directories src/pkga, src/pkga/pkgb. Fully qualified names are needed if you want to use a class that has the same name in two different packages you are importing.

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: import java.awt.*; okay import java.awt.*; does not import java.awt.Event.*

Input and Output Streams Streams handle Java input and output. Files are used to connect a stream to a file on the disk rather the keyboard or screen

File I/O Concepts Files allow programs to access persistent data. Data in files is often structured as an ordered list of records. Each record generally contains an ordered list of fields. Example: a file of student records where each record contains a name, major, catalog year: Doe, Jane cs 2017 McGee Jimbob business 2015 Records may or may not be fixed length and contain the same number of fields, but often they do.

File I/O Concepts, con’t File open operations: open, close. Java file traversal methodologies are: Sequential (all except RandomAccessFile) and Random (RandomAccessFile), Kind of data files hold: Text (characters only), Binary (characters and/or numeric data) and Object (serialized representations of java objects) File class: File file = new File(“pathname”) makes a java object describing a particular file on the disk if (!file.exists())  file doesn’t exist on the disk. Exceptions thrown by methods that deal with files. FileNotFoundException, IOException, EOFException

Sequential Text File Streams Operations: sequential reads and writes of chars BufferedReader in = new BufferedReader ( new FileReader(“path”)); strVar = in.readLine(); in.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, but it consumes input if not at end of file. The mark() method will throw an exception if there is no more data to read. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(“path”))); out.println(“data”); out.close(); PrintWriter writes chars to text files.

Binary File Streams Operations: Reads and Writes of raw bytes of all primitive types 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(); UTF stands for Unicode Text Format for writing and reading chars in a compact way. (See interface java.io.DataInput for the representation.) DataOutputStream wraps FileOutputStream to provide more methods for writing data of different types. See demos/WriteDemo.java Hexdump - https://www.di-mgt.com.au/hexdump-for-windows.html very useful for looking at binary files. Mac – terminal – hexdump.

Random File Read and Write RandomAccessFile raf = new RandomAccessFile(“path”,”access”); Acts like both a stream and a file, i.e., doesn’t need an additional stream class. 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 See demos/RAFDemo.java

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"); JFileChooser is part of javax.swing – a set of classes for building Graphical User Interfaces (GUI) See bottom of demos/WriteDemo.java