Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.*;

public class Main {

    public static void main(String[] args) {
        try {
            byte[] b = { 1, 2, 3, 4, 5, 6 };

            RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

            // write 2 bytes in the file
            raf.write(b, 2, 2);

            // set the file pointer at 0 position
            raf.seek(0);

            // print the two bytes we wrote
            System.out.println(raf.readByte());
            System.out.println(raf.readByte());
            raf.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}