RandomAccessFile Introduction : RandomAccessFile « File « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » File » RandomAccessFile 
11.40.1.RandomAccessFile Introduction
  1. A RandomAccessFile employs an internal pointer that points to the next byte to read.
  2. This pointer is zero-based and the first byte is indicated by index 0.
  3. When first created, a RandomAccessFile points to the first byte.
  4. You can change the pointer's position by invoking the seek method.
  5. The skipBytes method moves the pointer by the specified number of bytes.
  6. If skipping offset number of bytes would pass the end of file, the internal pointer will only move to as much as the end of file.
  7. The skipBytes method returns the actual number of bytes skipped.

When opening a file using a RandomAccessFile, you can choose whether to open it read-only or read write.

public RandomAccessFile (File file, String modethrows FileNotFoundException
public RandomAccessFile (String filePath, String modethrows FileNotFoundException

The value of mode can be one of these:

"r".     Open for reading only.

      "rw".    Open for reading and writing. 
               If the file does not already exist, RandomAccessFile creates the file.

      "rws".   Open for reading and writing and require that every update to the file's content and metadata be written synchronously.

      "rwd".   Open for reading and writing and require that every update to the file's content (but not metadatabe written synchronously.
11.40.RandomAccessFile
11.40.1.RandomAccessFile Introduction
11.40.2.Employs RandomAccessFile to store ints and changes the value of the third int.
11.40.3.Seek in RandomAccessFile
11.40.4.Getting FileChannel from RandomAccessFile
11.40.5.Write int to RandomAccessFile using FileChannel
11.40.6.Use RandomAccessFile to save and read
11.40.7.Use RandomAccessFile to reverse a file
11.40.8.Test file pointer manipulation between FileChannel and RandomAccessFile objects.
11.40.9.Appending data to existing file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.