Hello,

Here's an example of using RandomAccessFile. I hope it helps

package randomaccessfileproject;

/**
 *
 * @author Andreea
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileExample {

    public static void main(String[] args) {
        try {

            /* Create a new instance of RandomAccessFile class. We'll
do a "r"
             * read and "w" write operation to the file.
             * We are going to use the RandonAccessFile constructor:
             * RandomAccessFile(String name, String mode)
             * that creates a random access file stream to read from
and to
             * write to a file with the specified name("names.dat").
            */
            RandomAccessFile raf = new RandomAccessFile("names.dat",
"rw");

            // We'll define an array of Strings containing five names.
            String names[] = new String[5];
            names[0] = "John Doe";
            names[1] = "Jane Fonda";
            names[2] = "Farrah Fawcett";
            names[3] = "Keira Knightley";
            names[4] = "Johnny Cash";

            for (int i = 0; i < names.length; i++) {
                /* writeUTF(String str)
                 * Writes a string to the file using UTF-8 encoding
                 * in a machine-independent manner.
                 */
                raf.writeUTF(names[i]);
            }

            /* Write another data at the end of the file.
               The file pointer can be read by the getFilePointer
method and
               set by the seek method
            */
            raf.seek(raf.length());
            raf.writeUTF("Another data");

            // Move the file pointer to the beginning of the file
            raf.seek(0);

            /* While the file pointer is less than the file length,
read the
             next strings of data file from the current position of
the
             file pointer. */

            while (raf.getFilePointer() < raf.length()) {
                System.out.println(raf.readUTF()); // Reads a String
fron this file
            }
        } catch (FileNotFoundException e) {
            // FileNotFoundException can be thrown by the
RandomAccessFile constructor.
            e.printStackTrace();
        } catch (IOException e) {
            // If any byte cannot be read for any reason other than
end-of-file, an IOException is thrown.
            // In particular, an IOException may be thrown if the
stream has been closed.
            e.printStackTrace();
        }
    }

}

On Nov 26, 1:57 pm, Programerz <[email protected]> wrote:
> Hello to All java masters,
> I need help in using RandomAcessFile class in java.
> From what I learned it can read and write files with some sort like
> pointer or index.
> does anybody can explain how to use it?

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to