Binary File I/O: Writing and Reading Raw Data in Java, Slides of Java Programming

An introduction to working with binary files in java, including how to write and read raw data using the file and randomaccessfile classes. Topics covered include creating and deleting files, checking permissions, writing and reading integers, doubles, characters, strings, and classes, and handling exceptions when reading. Random file access is also discussed.

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarmistha
sarmistha 🇮🇳

3.8

(22)

112 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Files
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Binary File I/O: Writing and Reading Raw Data in Java and more Slides Java Programming in PDF only on Docsity!

Binary Files

Announcements

Homework 5 due on Monday

File class

The File class allows you to do basic operations with files, such as:

  • check if a file/directory exists
  • make directories/files
  • delete directories/files
  • check/modify permissions on files
  • rename files (See: FileClassExample.java) Docsity.com

Binary files

So far we have been dealing with “text” files which store characters Sometimes it is more convenient to use “binary” files, which store raw data Unfortunately, binary files are hard to read if you don't know their format

Writing to binary files

When writing to text files, we used println() However, to write to data files you need to run a specific method for each type of thing: writeInt(), writeDouble(), writeChar(), ... writeUTF() is for writing Strings writeObject is for writing classes/arrays (See: WriteBinaryFile.java) Docsity.com

Writing class objects

In order to write classes to binary files, you need to add something to your class definition: All you need to put is “implements Serializable” and you can write your class to a file (if you have an instance of another class in yours, that class also needs to be Serializable) (We will learn about “implements”in Ch 13)

EoF in binary files

Unfortunately, there is no way to tell if you are at the end of a binary file Instead you need to catch EOFException (when there is no more data) You can get other exceptions if you try to read the incorrect type of data (See: ReadBinaryFileBad.java) Docsity.com

Binary vs Text

Text files:

  • Stores readable characters
  • Need to parse (might not be easy)
  • Useful when interacting applications Binary files:
  • Stores raw data
  • Need to know order/type of data
  • Use when storing data for your own code

Random file access

So far, we have been...

  • starting at the beginning of the file
  • can only move forward (one object at a time) Random file access allows us to skip different places in a file, along with the capability to both read and write

Random file access

Random access is done by storing a file pointer (much like an array index, starting at 0) The file pointer starts at the beginning of the file (be careful not to write over old data) New methods with random access: seek() - moves to that spot in the file length() - length of the file getFilePointer() - where you are in the fileDocsity.com

Random file access

Unfortunately, RandomAccessFile class does not have readObject() and writeObject() The file pointer is annoying because it is the actual bytes, instead of index For example: an int is four bytes, so if you write 2 ints, the first one starts at spot 0 and the second one at spot 4 (See: RandomAccess.java)