Example usage for java.io RandomAccessFile writeInt

List of usage examples for java.io RandomAccessFile writeInt

Introduction

In this page you can find the example usage for java.io RandomAccessFile writeInt.

Prototype

public final void writeInt(int v) throws IOException 

Source Link

Document

Writes an int to the file as four bytes, high byte first.

Usage

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");
    raf.writeInt(1);
    for (int i = 0; i < 10; i++) {
        raf.seek(raf.length() - 4);//from ww  w  .  j ava2s  . c o  m
        raf.writeInt(raf.readInt());
    }
    raf.close();

}

From source file:InputOutputDemoPrimitive.java

public static void main(String[] a) throws Exception {

    //Read and write parts of file "raf.dat" in arbitrary order:
    RandomAccessFile raf = new RandomAccessFile("r.dat", "rw");
    raf.writeDouble(3.1415);/*from   w  w  w.  java 2 s .com*/
    raf.writeInt(42);
    raf.seek(0);
    System.out.println(raf.readDouble() + " " + raf.readInt());

}

From source file:Main.java

public static void main(String[] args) {
    try {/* ww  w. ja v  a  2s .  c o  m*/
        int f = 1234;

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

        raf.writeInt(f);

        raf.seek(0);

        System.out.println(raf.readInt());

        raf.seek(0);

        raf.writeInt(200);

        raf.seek(0);

        System.out.println(raf.readInt());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from  w w w  . ja  va2 s  . c o  m
        RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw");
        raf.writeInt(10);
        raf.writeInt(43);
        raf.writeInt(88);
        raf.writeInt(455);

        raf.seek((3 - 1) * 4);
        raf.writeInt(99);
        raf.seek(0);
        int i = raf.readInt();
        while (i != -1) {
            System.out.println(i);

            i = raf.readInt();
        }
        raf.close();
    } catch (IOException e) {
    }
}

From source file:RandomIOApp.java

public static void main(String args[]) throws IOException {
    RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
    file.writeBoolean(true);/*from w  ww .ja  va 2  s .co  m*/
    file.writeInt(123456);
    file.writeChar('j');
    file.writeDouble(1234.56);
    file.seek(1);
    System.out.println(file.readInt());
    System.out.println(file.readChar());
    System.out.println(file.readDouble());
    file.seek(0);
    System.out.println(file.readBoolean());
    file.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from www. j a v a2  s .  c  o  m
        RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw");
        raf.writeInt(10);
        raf.writeInt(43);
        raf.writeInt(88);
        raf.writeInt(455);

        // change the 3rd integer from 88 to 99
        raf.seek((3 - 1) * 4);
        raf.writeInt(99);
        raf.seek(0); // go to the first integer
        int i = raf.readInt();
        while (i != -1) {
            System.out.println(i);

            i = raf.readInt();
        }
        raf.close();
    } catch (IOException e) {
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//  w  w w. j  ava 2 s.c  o  m
        int i = 123;

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

        // write something in the file
        raf.writeInt(123);

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

        // print the int
        System.out.println(raf.readInt());

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

        // write something in the file
        raf.writeInt(i);

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

        // print the int
        System.out.println(raf.readInt());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:VASSAL.launch.ModuleManager.java

public static void main(String[] args) {
    // FIXME: We need to catch more exceptions in main() and then exit in
    // order to avoid situations where the main thread ends due to an uncaught
    // exception, but there are other threads still running, and so VASSAL
    // does not quit. For example, this can happen if an IllegalArgumentException
    // is thrown here...

    // parse command-line arguments
    LaunchRequest lr = null;//  w  ww  . ja  v  a 2  s.c  om
    try {
        lr = LaunchRequest.parseArgs(args);
    } catch (LaunchRequestException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: " + e.getMessage());
        System.exit(1);
    }

    // do this before the graphics subsystem fires up or it won't stick
    System.setProperty("swing.boldMetal", "false");

    if (lr.mode == LaunchRequest.Mode.TRANSLATE) {
        // show the translation window in translation mode
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // FIXME: does this window exit on close?
                new TranslateVassalWindow(null).setVisible(true);
            }
        });
        return;
    }

    //
    // How we start exactly one request server:
    //
    // To ensure that exactly one process acts as the request server, we
    // acquire a lock on the ~/VASSAL/key file, and then attempt to acquire
    // a lock on the ~/VASSAL/lock file. If we cannot lock ~/VASSAL/lock,
    // then there is already a server running; in that case, we read the
    // port number and security key from ~/VASSAL/key. If we can lock
    // ~/VASSAL/lock, then we start the server, write the port number and
    // key to ~/VASSAL/key, and continue to hold the lock on ~/VASSAL/lock.
    // Finally, we unlock ~/VASSAL/key and proceed to act as a client,
    // sending requests over localhost:port using the security key.
    //
    // The advantages of this method are:
    //
    // (1) No race conditions between processes started at the same time.
    // (2) No port collisions, because we don't use a predetermined port.
    //

    final File keyfile = new File(Info.getConfDir(), "key");
    final File lockfile = new File(Info.getConfDir(), "lock");

    int port = 0;
    long key = 0;

    RandomAccessFile kraf = null;
    FileLock klock = null;
    try {
        // acquire an exclusive lock on the key file
        kraf = new RandomAccessFile(keyfile, "rw");

        try {
            klock = kraf.getChannel().lock();
        } catch (OverlappingFileLockException e) {
            throw (IOException) new IOException().initCause(e);
        }

        // determine whether we are the server or a client

        // Note: We purposely keep lout open in the case where we are the
        // server, because closing lout will release the lock.
        FileLock lock = null;
        final FileOutputStream lout = new FileOutputStream(lockfile);
        try {
            lock = lout.getChannel().tryLock();
        } catch (OverlappingFileLockException e) {
            throw (IOException) new IOException().initCause(e);
        }

        if (lock != null) {
            // we have the lock, so we will be the request server

            // bind to an available port on the loopback device
            final ServerSocket serverSocket = new ServerSocket(0, 0, InetAddress.getByName(null));

            // write the port number where we listen to the key file
            port = serverSocket.getLocalPort();
            kraf.writeInt(port);

            // create new security key and write it to the key file
            key = (long) (Math.random() * Long.MAX_VALUE);
            kraf.writeLong(key);

            // create a new Module Manager
            new ModuleManager(serverSocket, key, lout, lock);
        } else {
            // we do not have the lock, so we will be a request client
            lout.close();

            // read the port number we will connect to from the key file
            port = kraf.readInt();

            // read the security key from the key file
            key = kraf.readLong();
        }

        kraf.close();
    } catch (IOException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: IO error");
        e.printStackTrace();
        System.exit(1);
    } finally {
        // this will also release the lock on the key file
        IOUtils.closeQuietly(kraf);
    }

    lr.key = key;

    // pass launch parameters on to the ModuleManager via the socket
    Socket clientSocket = null;
    ObjectOutputStream out = null;
    InputStream in = null;
    try {
        clientSocket = new Socket((String) null, port);

        out = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream()));
        out.writeObject(lr);
        out.flush();

        in = clientSocket.getInputStream();
        IOUtils.copy(in, System.err);
    } catch (IOException e) {
        // FIXME: should be a dialog...
        System.err.println("VASSAL: Problem with socket on port " + port);
        e.printStackTrace();
        System.exit(1);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly((Closeable) out);
        IOUtils.closeQuietly(clientSocket);
    }
}

From source file:Main.java

public static void initialWrite(String fileName) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
    raf.writeInt(0);
    raf.writeUTF("Hello world!");
    raf.close();//from  www  . j  a v a  2 s. c o m
}

From source file:Main.java

public static void incrementReadCounter(RandomAccessFile raf) throws IOException {
    long currentPosition = raf.getFilePointer();
    raf.seek(0);/*  w  ww . jav  a 2  s  .  com*/
    int counter = raf.readInt();
    counter++;
    raf.seek(0);
    raf.writeInt(counter);
    raf.seek(currentPosition);
}