Binary Files, Random Access Files. 12-2 Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 12: Exceptions and Advanced File I/O Starting Out with Java: From Control Structures through.
Starting Out With Java 5 (Early Objects) Chapter 10 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
12-2 Chapter Topics Chapter 12 discusses the following main topics: Part I: Exceptions Handling Exceptions Throwing Exceptions Part II: More about Input/Output.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
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.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
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.
Random Access Files Sandeep Patel. Random Access Files Random access files are files in which records can be accessed in any order –Also called direct.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary 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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
CSC – Java Programming II Lecture 9 January 30, 2002.
Exceptions and Advanced File I/O
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
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 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,
Files and Streams. Midterm exam Time: Wednesday, October 31, 2007 Format: Multiple choices (about 15 to 20 questions) Determine the results of the code.
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.
Working with files. RHS – SOC 2 Motivation All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 10 Exceptions and Advanced File I/O.
A stream is a sequence of data. A stream is a flowing sequence of characters.
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.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
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.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 10: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Files in Java 1.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Java IO Exploring the java.io package and living to talk about it.
Chapter 12 Exceptions and Advanced File I/O. 2 Contents I. Handling Exceptions II. Throwing Exceptions III. Advanced Topics 1. Binary Files 2. Random.
The Java IO System Different kinds of IO Different kinds of operations
Last Time Reviewed method overloading. A few useful Java classes:
Chapter 14: Sequential Access Files
Chapter 12: Exceptions and Advanced File I/O
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 17 Binary I/O.
Exceptions and Advanced File I/O
I/O Basics.
Chapter 17 Binary I/O 1.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Chapter 11: Exceptions and Advanced File I/O
Topics Introduction to File Input and Output
MSIS 670: Object-Oriented Software Engineering
Computer Programming with JAVA
Topics Introduction to File Input and Output
CS 240 – Advanced Programming Concepts
Topics Introduction to File Input and Output
The Package Statement Group related interfaces and classes together
David Davenport Spring 2005
Presentation transcript:

Binary Files, Random Access Files

12-2 Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in a file in its raw binary format. A file that contains binary data is often called a binary file. Storing data in its binary format is more efficient than storing it as text. There are some types of data that should only be stored in its raw binary format.

12-3 Binary Files Binary files cannot be opened in a text editor such as Notepad. To write data to a binary file you must create objects from the following classes: – FileOutputStream - allows you to open a file for writing binary data. It provides only basic functionality for writing bytes to the file. – DataOutputStream - allows you to write data of any primitive type or String objects to a binary file. Cannot directly access a file. It is used in conjunction with a FileOutputStream object that has a connection to a file.

12-4 Binary Files A DataOutputStream object is wrapped around a FileOutputStream object to write data to a binary file. FileOutputStream fstream = new FileOutputStream("MyInfo.dat"); DataOutputStream outputFile = new DataOutputStream(fstream); If the file that you are opening with the FileOutputStream object already exists, it will be erased and an empty file by the same name will be created.

12-5 Binary Files These statements can combined into one. DataOutputStream outputFile = new DataOutputStream(new FileOutputStream("MyInfo.dat")); Once the DataOutputStream object has been created, you can use it to write binary data to the file. Example: WriteBinaryFile.javaWriteBinaryFile.java

12-6 Binary Files To open a binary file for input, you wrap a DataInputStream object around a FileInputStream object. FileInputStream fstream = new FileInputStream("MyInfo.dat"); DataInputStream inputFile = new DataInputStream(fstream); These two statements can be combined into one. DataInputStream inputFile = new DataInputStream(new FileInputStream("MyInfo.dat"));

12-7 Binary Files The FileInputStream constructor will throw a FileNotFoundException if the file named by the string argument cannot be found. Once the DataInputStream object has been created, you can use it to read binary data from the file. Example: – ReadBinaryFile.java ReadBinaryFile.java

12-8 Writing and Reading Strings To write a string to a binary file, use the DataOutputStream class’s writeUTF method. This method writes its String argument in a format known as UTF-8 encoding. – Just before writing the string, this method writes a two- byte integer indicating the number of bytes that the string occupies. – Then, it writes the string’s characters in Unicode. (UTF stands for Unicode Text Format.) The DataInputStream class’s readUTF method reads from the file.

12-9 Writing and Reading Strings To write a string to a file: String name = "Chloe"; outputFile.writeUTF(name); To read a string from a file: String name = inputFile.readUTF(); The readUTF method will correctly read a string only when the string was written with the writeUTF method. Example: – WriteUTF.java WriteUTF.java – ReadUTF.java ReadUTF.java

12-10 Appending Data to Binary Files The FileOutputStream constructor takes an optional second argument which must be a boolean value. If the argument is true, the file will not be erased if it exists; new data will be written to the end of the file. If the argument is false, the file will be erased if it already exists. FileOutputStream fstream = new FileOutputStream("MyInfo.dat", true); DataOutputStream outputFile = new DataOutputStream(fstream);

12-11 Random Access Files Text files and the binary files previously shown use sequential file access. With sequential access: – The first time data is read from the file, the data will be read from its beginning. – As the reading continues, the file’s read position advances sequentially through the file’s contents. Sequential file access is useful in many circumstances. If the file is very large, locating data buried deep inside it can take a long time.

12-12 Random Access Files Java allows a program to perform random file access. In random file access, a program may immediately jump to any location in the file. To create and work with random access files in Java, you use the RandomAccessFile class. RandomAccessFile(String filename, String mode) – filename : the name of the file. – mode : a string indicating the mode in which you wish to use the file. "r" = reading "rw" = for reading and writing.

12-13 Random Access Files // Open a file for random reading. RandomAccessFile randomFile = new RandomAccessFile("MyData.dat", "r"); // Open a file for random reading and writing. RandomAccessFile randomFile = new RandomAccessFile("MyData.dat", "rw"); When opening a file in "r" mode where the file does not exist, a FileNotFoundException will be thrown. Opening a file in "r" mode and trying to write to it will throw an IOException. If you open an existing file in "rw" mode, it will not be deleted and the file’s existing content will be preserved.

12-14 Random Access Files Items in a sequential access file are accessed one after the other. Items in a random access file are accessed in any order. If you open a file in "rw" mode and the file does not exist, it will be created. A file that is opened or created with the RandomAccessFile class is treated as a binary file.

12-15 Random Access Files The RandomAccessFile class has: – the same methods as the DataOutputStream class for writing data, and – the same methods as the DataInputStream class for reading data. The RandomAccessFile class can be used to sequentially process a binary file. Example: WriteLetters.javaWriteLetters.java

12-16 The File Pointer The RandomAccessFile class treats a file as a stream of bytes. The bytes are numbered: – the first byte is byte 0. – The last byte’s number is one less than the number of bytes in the file. These byte numbers are similar to an array’s subscripts, and are used to identify locations in the file. Internally, the RandomAccessFile class keeps a long integer value known as the file pointer.

12-17 The File Pointer The file pointer holds the byte number of a location in the file. When a file is first opened, the file pointer is set to 0. When an item is read from the file, it is read from the byte that the file pointer points to. Reading also causes the file pointer to advance to the byte just beyond the item that was read. If another item is immediately read, the reading will begin at that point in the file.

12-18 The File Pointer An EOFException is thrown when a read causes the file pointer to go beyond the size of the file. Writing also takes place at the location pointed to by the file pointer. If the file pointer points to the end of the file, data will be written to the end of the file. If the file pointer holds the number of a byte within the file, at a location where data is already stored, a write will overwrite the data at that point.

12-19 The File Pointer The RandomAccessFile class lets you move the file pointer. This allows data to be read and written at any byte location in the file. The seek method is used to move the file pointer. rndFile.seek(long position); The argument is the number of the byte that you want to move the file pointer to.

12-20 The File Pointer RandomAccessFile file = new RandomAccessFile("MyInfo.dat", "r"); file.seek(99); byte b = file.readByte(); Example: ReadRandomLetters.javaReadRandomLetters.java