Chapter 17 Binary I/O.

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

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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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.
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.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Binary I/O Outline Text I/O Chap 9. WriteData Chap 9. ReadData Binary I/O Text I/O vs Binary I/O Binary I/O Classes Using DataInputStream/DataOutputStream.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary 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.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
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 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013 Reference books: Malik D.S., Java Programming, From Problem Analysis to Program Design,
Introduction to Java Y.Daniel Liang
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.
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.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 16 Simple Input and Output 与人玫瑰,手有余香.
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 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.
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.
Object Persistence and Object serialization CSNB534 Asma Shakil.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
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.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
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.
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.
Fundamental of Java Programming
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
CIS265/506 Cleveland State University – Prof. Victor Matos
OO Design and Programming II I/O: Reading and Writing
Ch14 Files and Streams OBJECTIVES
Chapter 17 Binary I/O.
Object Writing in files
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Chapter 17 Binary I/O 1.
Programming in Java Files and I/O Streams
Streams and File I/O Chapter 9 Chapter 9.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
MSIS 670: Object-Oriented Software Engineering
Chapter 16 Simple Input and Output
David Davenport Spring 2005
Presentation transcript:

Chapter 17 Binary I/O

Motivations Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are designed to be read by programs. For example, Java source programs are stored in text files and can be read by a text editor, but Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files.

Objectives To discover how I/O is processed in Java (§17.2). To distinguish between text I/O and binary I/O (§17.3). To read and write bytes using FileInputStream and FileOutputStream (§17.4.1). To read and write primitive values and strings using DataInputStream/DataOutputStream (§17.4.3). To store and restore objects using ObjectOutputStream and ObjectInputStream, and to understand how objects are serialized and what kind of objects can be serialized (§17.6). To implement the Serializable interface to make objects serializable (§17.6.1). To serialize arrays (§17.6.2). To read and write the same file using the RandomAccessFile class (§17.7).

How is I/O Handled in Java? A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. Scanner input = new Scanner(new File("temp.txt")); System.out.println(input.nextLine()); input and output objects are called input and output streams. PrintWriter output = new PrintWriter("temp.txt"); output.println("Java 101"); output.close();

Text File vs. Binary File Data stored in a text file are represented in human-readable form. Data stored in a binary file are represented in binary form. You cannot read binary files. Binary files are designed to be read by programs. For example, the Java source programs are stored in text files and can be read by a text editor, but the Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files.

Text File vs. Binary File Although it is not technically precise and correct, you can imagine that a text file consists of a sequence of characters and a binary file consists of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters: '1', '9', '9' in a text file and the same integer is stored as a byte-type value C7 in a binary file, because decimal 199 equals to hex C7. Characters in a text file are encoded using a character-encoding scheme such as ASCII or Unicode

Binary I/O Text I/O requires encoding and decoding. The JVM converts a Unicode to a file specific encoding when writing a character and coverts a file specific encoding to a Unicode when reading a character. Binary I/O does not require conversions. When you write a byte to a file, the original byte is copied into the file. When you read a byte from a file, the exact byte in the file is returned.

Binary I/O Classes

InputStream The value returned is a byte as an int type.

OutputStream The value is a byte as an int type.

FileInputStream/FileOutputStream FileInputStream/FileOutputStream associates a binary input/output stream with an external file. All the methods in FileInputStream/FileOuptputStream are inherited from its superclasses.

FileInputStream To construct a FileInputStream, use the following constructors: public FileInputStream(String filename) public FileInputStream(File file) A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file.

FileOutputStream To construct a FileOutputStream, use the following constructors: public FileOutputStream(String filename) public FileOutputStream(File file) public FileOutputStream(String filename, boolean append) public FileOutputStream(File file, boolean append)   If the file does not exist, a new file would be created. If the file already exists, the first two constructors would delete the current contents in the file. To retain the current content and append new data into the file, use the last two constructors by passing true to the append parameter. TestFileStream Run

1  import java.io.*; 2   3  public class TestFileStream { 4   public static void main(String[] args) throws IOException { 5   try ( 6   // Create an output stream to the file 7   FileOutputStream output = new FileOutputStream("temp.dat"); 8   ) { 9   // Output values to the file 10   for (int i = 1; i <= 10; i++) 11   output.write(i); 12   } 13   14   try ( 15   // Create an input stream for the file 16   FileInputStream input = new FileInputStream("temp.dat"); 17   ) { 18   // Read values from the file 19   int value; 20   while ((value = input.read()) != -1) 21   System.out.print(value + " "); 22   } 23   } 24  } 1 2 3 4 5 6 7 8 9 10

Binary Files The file temp.dat created in this example is a binary file. It can be read from a Java program but not from a text editor

AutoClosable The java.io.InputStream and java.io.OutputStream classes implement the AutoClosable interface. The AutoClosable interface defines the close() method that closes a resource. Any object of the AutoClosable type can be used with the try-with-resources syntax for automatic closing.

Appending to Text File An instance of FileInputStream can be used as an argument to construct a Scanner An instance of FileOutputStream can be used as an argument to construct a PrintWriter. You can create a PrintWriter to append text into a file using If temp.txt does not exist, it is created. If temp.txt already exists, new data are appended to the file new PrintWriter(new FileOutputStream("temp.txt", true));

FilterInputStream/FilterOutputStream Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can only be used for reading bytes. If you want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream. Using a filter class enables you to read integers, doubles, and strings instead of bytes and characters. FilterInputStream and FilterOutputStream are the base classes for filtering data. When you need to process primitive numeric types, use DatInputStream and DataOutputStream to filter bytes.

DataInputStream/DataOutputStream DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings. DataOutputStream converts primitive type values or strings into bytes and output the bytes to the stream.

DataInputStream DataInputStream extends FilterInputStream and implements the DataInput interface.

DataInputStream This class, which is in the java.io package, allows you to read data of any primitive type, or String objects, from a binary file. The DataInputStream class by itself cannot directly access a file, however. It is used in conjunction with a FileInputStream object that has a connection to a file.

DataOutputStream DataOutputStream extends FilterOutputStream and implements the DataOutput interface.

DataOutputStream This class which is in the java.io package, allows you to write data of any primitive type or String objects to a binary file. The DataOutputStream class by itself cannot directly access a file, however. It is used in conjunction with a FileOutputStream object that has a connection to a file.

Characters and Strings in Binary I/O (1) A Unicode consists of two bytes. The writeChar(char c) method writes the Unicode of character c to the output. The writeChars(String s) method writes the Unicode for each character in the string s to the output.

Characters and Strings in Binary I/O (2) What is UTF-8? UTF-8 is a coding scheme that allows systems to operate with both ASCII and Unicode efficiently. Most operating systems use ASCII. Java uses Unicode. The ASCII character set is a subset of the Unicode character set.

Characters and Strings in Binary I/O (3) Why UTF-8? Since most applications need only the ASCII character set, it is a waste to represent an 8-bit ASCII character as a 16-bit Unicode character. The UTF-8 is an alternative scheme that stores a character using 1, 2, or 3 bytes. ASCII values (less than 0x7F) are coded in one byte. Unicode values less than 0x7FF are coded in two bytes. Other Unicode values are coded in three bytes.

Using DataInputStream/DataOutputStream Data streams are used as wrappers on existing input and output streams to filter data in the original stream. They are created using the following constructors: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream)   The statements given below create data streams. The first statement creates an input stream for file in.dat; the second statement creates an output stream for file out.dat. DataInputStream infile = new DataInputStream(new FileInputStream("in.dat")); DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); TestDataStream Run

John 85.5 Susan 185.5 Kim 105.25 1 import java.io.*; 2 2   3  public class TestDataStream { 4   public static void main(String[] args) throws IOException { 5   try ( // Create an output stream for file temp.dat 6   DataOutputStream output = 7   new DataOutputStream(new FileOutputStream("temp.dat")); 8   ) { 9   // Write student test scores to the file 10   output.writeUTF("John"); 11   output.writeDouble(85.5); 12   output.writeUTF("Jim"); 13   output.writeDouble(185.5); 14   output.writeUTF("George"); 15   output.writeDouble(105.25); 16   } 17   18   try ( // Create an input stream for file temp.dat 19   DataInputStream input = 20   new DataInputStream(new FileInputStream("temp.dat")); 21   ) { 22   // Read student test scores from the file 23   System.out.println(input.readUTF() + " " + input.readDouble()); 24   System.out.println(input.readUTF() + " " + input.readDouble()); 25   System.out.println(input.readUTF() + " " + input.readDouble()); 26   } 27   } 28  } John 85.5 Susan 185.5 Kim 105.25

Concept of pipe line

Order and Format Checking End of File CAUTION: You have to read the data in the same order and same format in which they are stored. For example, since names are written in UTF-8 using writeUTF, you must read names using readUTF. Checking End of File TIP: If you keep reading data at the end of a stream, an EOFException would occur. So how do you check the end of a file? You can use input.available() to check it. input.available() == 0 indicates that it is the end of a file.

4.5 43.25 3.2 All data were read 1 import java.io.*; 2 2   3  public class DetectEndOfFile { 4   public static void main(String[] args) { 5   try { 6   try (DataOutputStream output = 7   new DataOutputStream(new FileOutputStream("test.dat"))) { 8   output.writeDouble(4.5); 9   output.writeDouble(43.25); 10   output.writeDouble(3.2); 11   } 12   13   try (DataInputStream input = 14   new DataInputStream(new FileInputStream("test.dat"))) { 15   while (true) 16   System.out.println(input.readDouble()); 17   } 18   } 19   catch (EOFException ex) { 20   System.out.println("All data were read"); 21   } 22   catch (IOException ex) { 23   ex.printStackTrace(); 24   } 25   } 26  } 4.5 43.25 3.2 All data were read

BufferedInputStream/ BufferedOutputStream Using buffers to speed up I/O BufferedInputStream/BufferedOutputStream does not contain new methods. All the methods BufferedInputStream/BufferedOutputStream are inherited from the InputStream/OutputStream classes.

BufferedInputStream/ BufferedOutputStream Using BufferedInputStream, the whole block of data on the disk is read into the buffer in the memory once. The individual data are then loaded to your program from the buffer. Using BufferedOutputStream, the individual data are first written to the buffer in the memory. When the buffer is full, all data in the buffer are written to the disk once.

Constructing BufferedInputStream/BufferedOutputStream You can wrap a BufferedInputStream/BufferedOutputStream on any InputStream/ OutputStream using the constructors // Create a BufferedInputStream public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int bufferSize)   // Create a BufferedOutputStream public BufferedOutputStream(OutputStream out) public BufferedOutputStream(OutputStreamr out, int bufferSize)

Case Studies: Copy File This case study develops a program that copies files. The user needs to provide a source file and a target file as command-line arguments using the following command: java Copy source target The program copies a source file to a target file and displays the number of bytes in the file. If the source does not exist, tell the user the file is not found. If the target file already exists, tell the user the file already exists. Copy Run

1  import java.io.*; 2   3  public class Copy { 4   /** Main method 5   @param args[0] for sourcefile 6   @param args[1] for target file 7   */ 8   public static void main(String[] args) throws IOException { 9   // Check command-line parameter usage 10   if (args.length != 2) { 11   System.out.println( 12   "Usage: java Copy sourceFile targetfile"); 13   System.exit(1); 14   } 15   16   // Check if source file exists 17   File sourceFile = new File(args[0]); 18   if (!sourceFile.exists()) { 19   System.out.println("Source file " + args[0] 20   + " does not exist"); 21   System.exit(2); 22   }

24   // Check if target file exists 25   File targetFile = new File(args[1]); 26   if (targetFile.exists()) { 27   System.out.println("Target file " + args[1] 28   + " already exists"); 29   System.exit(3); 30   } 31   32   try ( 33   // Create an input stream 34   BufferedInputStream input = 35   new BufferedInputStream(new FileInputStream(sourceFile)); 36   37   // Create an output stream 38   BufferedOutputStream output = 39   new BufferedOutputStream(new FileOutputStream(targetFile)); 40   ) { 41   // Continuously read a byte from input and write it to output 42   int r, numberOfBytesCopied = 0; 43   while ((r = input.read()) != -1) { 44   output.write((byte)r); 45   numberOfBytesCopied++; 46   } 47   48   // Display the file size 49   System.out.println(numberOfBytesCopied + " bytes copied"); 50   } 51   } 52  }

Optional Object I/O DataInputStream/DataOutputStream enables you to perform I/O for primitive type values and strings. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings.

ObjectInputStream ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants.

ObjectOutputStream ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants.

Using Object Streams You may wrap an ObjectInputStream/ObjectOutputStream on any InputStream/OutputStream using the following constructors: // Create an ObjectInputStream public ObjectInputStream(InputStream in)   // Create an ObjectOutputStream public ObjectOutputStream(OutputStream out) TestObjectOutputStream Run TestObjectInputStream Run

1  import java.io.*; 2   3  public class TestObjectOutputStream { 4   public static void main(String[] args) throws IOException { 5   try ( // Create an output stream for file object.dat 6   ObjectOutputStream output = 7   new ObjectOutputStream(new FileOutputStream("object.dat")); 8   ) { 9   // Write a string, double value, and object to the file 10   output.writeUTF("John"); 11   output.writeDouble(85.5); 12   output.writeObject(new java.util.Date()); 13   } 14   } 15  }

Notice that you must cast the return value to the desired class type 1  import java.io.*; 2   3  public class TestObjectInputStream { 4   public static void main(String[] args) 5   throws ClassNotFoundException, IOException { 6   try ( // Create an input stream for file object.dat 7   ObjectInputStream input = 8   new ObjectInputStream(new FileInputStream("object.dat")); 9   ) { 10   // Read a string, double value, and object from the file 11   String name = input.readUTF(); 12   double score = input.readDouble(); 13   java.util.Date date = (java.util.Date)(input.readObject()); 14   System.out.println(name + " " + score + " " + date); 15   } 16   } 17  } John 85.5 Sun Dec 04 10:35:31 EST 2011 Notice that you must cast the return value to the desired class type

The Serializable Interface Not all objects can be written to an output stream. Objects that can be written to an object stream is said to be serializable. A serializable object is an instance of the java.io.Serializable interface. So the class of a serializable object must implement Serializable. The Serializable interface is a marker interface. It has no methods, so you don't need to add additional code in your class that implements Serializable. Implementing this interface enables the Java serialization mechanism to automate the process of storing the objects and arrays.

The transient Keyword If an object is an instance of Serializable, but it contains non-serializable instance data fields, can the object be serialized? The answer is no. To enable the object to be serialized, you can use the transient keyword to mark these data fields to tell the JVM to ignore these fields when writing the object to an object stream.

The transient Keyword, cont. Consider the following class:   public class Foo implements java.io.Serializable { private int v1; private static double v2; private transient A v3 = new A(); } class A { } // A is not serializable When an object of the Foo class is serialized, only variable v1 is serialized. Variable v2 is not serialized because it is a static variable, and variable v3 is not serialized because it is marked transient. If v3 were not marked transient, a java.io.NotSerializableException would occur.

TestObjectStreamForArray Serializing Arrays An array is serializable if all its elements are serializable. So an entire array can be saved using writeObject into a file and later restored using readObject. Here is an example that stores an array of five int values and an array of three strings, and reads them back to display on the console. TestObjectStreamForArray Run

1  import java.io.*; 2   3  public class TestObjectStreamForArray { 4   public static void main(String[] args) 5   throws ClassNotFoundException, IOException { 6   int[] numbers = {1, 2, 3, 4, 5}; 7   String[] strings = {"John", "Susan", "Kim"}; 8   9   try ( // Create an output stream for file array.dat 10   ObjectOutputStream output = new ObjectOutputStream(new 11   FileOutputStream("array.dat", true)); 12   ) { 13   // Write arrays to the object output stream 14   output.writeObject(numbers); 15   output.writeObject(strings); 16   }

18   try ( // Create an input stream for file array.dat 19   ObjectInputStream input = 20   new ObjectInputStream(new FileInputStream("array.dat")); 21   ) { 22   int[] newNumbers = (int[])(input.readObject()); 23   String[] newStrings = (String[])(input.readObject()); 24   25   // Display arrays 26   for (int i = 0; i < newNumbers.length; i++) 27   System.out.print(newNumbers[i] + " "); 28   System.out.println(); 29   30   for (int i = 0; i < newStrings.length; i++) 31   System.out.print(newStrings[i] + " "); 32   } 33   } 34  } 1 2 3 4 5 John Susan Kim

Random Access Files All of the streams you have used so far are known as read-only or write-only streams. The external files of these streams are sequential files that cannot be updated without creating a new file. It is often necessary to modify files or to insert new records into files. Java provides the RandomAccessFile class to allow a file to be read from and write to at random locations.

RandomAccessFile

File Pointer A random access file consists of a sequence of bytes. There is a special marker called file pointer that is positioned at one of these bytes. A read or write operation takes place at the location of the file pointer. When a file is opened, the file pointer sets at the beginning of the file. When you read or write data to the file, the file pointer moves forward to the next data. For example, if you read an int value using readInt(), the JVM reads four bytes from the file pointer and now the file pointer is four bytes ahead of the previous location.

RandomAccessFile Methods Many methods in RandomAccessFile are the same as those in DataInputStream and DataOutputStream. For example, readInt(), readLong(), writeDouble(), readLine(), writeInt(), and writeLong() can be used in data input stream or data output stream as well as in RandomAccessFile streams.

RandomAccessFile Methods, cont. void seek(long pos) throws IOException; Sets the offset from the beginning of the RandomAccessFile stream to where the next read or write occurs. long getFilePointer() IOException; Returns the current offset, in bytes, from the beginning of the file to where the next read or write occurs.

RandomAccessFile Methods, cont. long length()IOException Returns the length of the file. final void writeChar(int v) throws IOException Writes a character to the file as a two-byte Unicode, with the high byte written first. final void writeChars(String s) throws IOException Writes a string to the file as a sequence of characters.

RandomAccessFile Constructor RandomAccessFile raf = new RandomAccessFile("test.dat", "rw"); // allows read and write RandomAccessFile raf = new RandomAccessFile("test.dat", "r"); // read only

A Short Example on RandomAccessFile TestRandomAccessFile Run

1  import java.io.*; 2   3  public class TestRandomAccessFile { 4   public static void main(String[] args) throws IOException { 5   try ( // Create a random access file 6   RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw"); 7   ) { 8   // Clear the file to destroy the old contents if exists 9   inout.setLength(0); 10   11   // Write new integers to the file 12   for (int i = 0; i < 200; i++) 13   inout.writeInt(i); 14   15   // Display the current length of the file 16   System.out.println("Current file length is " + inout.length()); 17   18   // Retrieve the first number 19   inout.seek(0); // Move the file pointer to the beginning 20   System.out.println("The first number is " + inout.readInt()); 21   22   // Retrieve the second number 23   inout.seek(1 * 4); // Move the file pointer to the second number 24   System.out.println("The second number is " + inout.readInt());

Current file length is 800 The first number is 0    26   // Retrieve the tenth number 27   inout.seek(9 * 4); // Move the file pointer to the tenth number 28   System.out.println("The tenth number is " + inout.readInt()); 29   30   // Modify the eleventh number 31   inout.writeInt(555); 32   33   // Append a new number 34   inout.seek(inout.length()); // Move the file pointer to the end 35   inout.writeInt(999); 36   37   // Display the new length 38   System.out.println("The new length is " + inout.length()); 39   40   // Retrieve the new eleventh number 41   inout.seek(10 * 4); // Move the file pointer to the eleventh number 42   System.out.println("The eleventh number is " + inout.readInt()); 43   } 44   } 45  } Current file length is 800 The first number is 0 The second number is 1 The tenth number is 9 The new length is 804 The eleventh number is 555