Podcast Ch23c Title: Binary Files

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 I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
Lecture 15: I/O and Parsing
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
Chapter 9 Streams and File I/O Overview of Streams and File I/O
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
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.
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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Java I/O Input: information brought to program from an external source
CSC – Java Programming II Lecture 9 January 30, 2002.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 23 Bit Arrays.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
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 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.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
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.
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.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
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.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Sequential files creation & writing
Podcast Ch23e Title: Implementing Huffman Compression
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
Introduction to programming in java
Chapter 17 Binary I/O.
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
CIS265/506 Files & Indexing CIS265/506: File Indexing.
I/O Basics.
Chapter 17 Binary I/O 1.
Accessing Files in Java
Testing and Exceptions
File I/O & collection frame work
Programming in Java Files and I/O Streams
CSS161: Fundamentals of Computing
Chapter 17 Binary I/O Dr. Clincy - Lecture.
JAVA IO.
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
Input and Output Stream
Podcast Ch23f Title: Serialization
Files and Streams in Java
Podcast Ch22b Title: Inserting into a Heap
Web Design & Development Lecture 8
Podcast Ch18a Title: Overview of Binary Search Trees
Java Basics Introduction to Streams.
CS 240 – Advanced Programming Concepts
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch23a Title: Bit Arrays
David Davenport Spring 2005
Presentation transcript:

Podcast Ch23c Title: Binary Files Description: Overview of binary files; DataInputStream; DataOutputStream; Program 23.1; file compression Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Binary Files File types are text files and binary files. Java deals with files by creating a byte stream that connects the file and the application. Binary files can be handled with DataInputStream and DataOutputStream classes.

Binary Files (continued) A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

The DataInputStream/DataOutputStream classes, provide methods for doing I/O of primitive types in _______ format. ASCII (b) Character binary (d) Unicode

Binary Files (continued)

Binary Files (continued)

Binary Files (continued)

Binary Files (continued)

Circle all classes, which are declared by Java as Abstract Classes OutputStream DataInputStream InputStream FileOutputStream ObjectInputStream

Program 23.1 (Run) int: 100 short: 1500 long: 4294967295 byte array: 3 5 2 7 15 100 127 55

Program 23.1 import java.io.*; public class Program23_1 { public static void main(String[] args) throws IOException int intVal = 100; short shortVal = 1500; long longVal = 4294967295L; byte[] buf = {3, 5, 2, 7, 15, 100, 127, 55}; // create a DataOutputStream that writes to // the file "data.dat" in the local directory DataOutputStream fout = null; // use to input data from "data.dat" DataInputStream fin = null;

Program 23.1 (continued) { fout = new DataOutputStream( try { fout = new DataOutputStream( new FileOutputStream("data.dat")); } catch (FileNotFoundException fnfe) System.err.println("Cannot create \"data.dat\""); System.exit(1); // write each variable and the array to f fout.writeInt(intVal); fout.writeShort(shortVal); fout.writeLong(longVal); fout.write(buf, 0, buf.length);

Program 23.1 (continued) // close the stream and open it // as a DataInputStream fout.close(); try { fin = new DataInputStream(new FileInputStream( "data.dat")); } catch (FileNotFoundException fnfe) { System.err.println("Failure to open " + "\"data.dat\""); System.exit(1); // input the int, short, and long from the file System.out.println("int: " + fin.readInt()); System.out.println("short: " + fin.readShort()); System.out.println("long: " + fin.readLong());

Program 23.1 (concluded) // input the byte array that was written // to the file; the number of bytes in the // array is the number of bytes remaining // unread in the file byte[] b = new byte[fin.available()]; System.out.print("byte array: "); // input the array fin.read(b); // output the bytes for (int i=0; i < b.length; i++) System.out.print(b[i] + " "); System.out.println(); // close the stream fin.close(); }

Which of the following statements correctly creates a DataOutputStream that writes data to the file named "out.dat"? (a) DataOutputStream dOut = new DataOutputStream ( new InputStream("out.dat")); (b) DataOutputStream dOut = new DataOutputStream( new FileOutputStream("out.dat")); (c) DataOutputStream dOut = new DataOutputStream ("out.dat"); (d) DataOutputStream dOut = (DataOutputStream) new FileOutputStream("out.dat"));

File Compression Lossless compression loses no data and is used for data backup.

File Compression (continued) Lossy compression is used for applications like sound and video compression and causes minor loss of data.

File Compression (continued) The compression ratio is the ratio of the number of bits in the original data to the number of bits in the compressed image. For instance, if a data file contains 500,000 bytes and the compressed data contains 100,000 bytes, the compression ratio is 5:1

Assume dOut is a DataOutputStream Assume dOut is a DataOutputStream. The "write" statements copy data for primitive byte, short, and int variables to a file. byte bVal = 75; short shVal = 40; int intVal = 101; dOut.writeByte(bVal); dOut.writeShort(shVal); dOut.writeInt(intVal); dOut.writeByte(bVal/20); dOut.writeShort(shVal*2); What is the contents of file as displayed in individual bytes with decimal values (a) 0 75 0 40 0 101 0 3 0 80 (b) 75 40 101 3 80 75 0 40 0 0 0 101 3 0 80 75 0 0 0 40 0 0 0 101 3 0 0 0 80