Example usage for java.io RandomAccessFile getFilePointer

List of usage examples for java.io RandomAccessFile getFilePointer

Introduction

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

Prototype

public native long getFilePointer() throws IOException;

Source Link

Document

Returns the current offset in this file.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile file = new RandomAccessFile("scores.html", "rw");
    for (int i = 1; i <= 6; i++) {
        System.out.println(file.readLine());
    }//from   w ww.  j a va  2 s . c  o  m
    long current = file.getFilePointer();
    file.seek(current + 6);
    file.write("34".getBytes());
    for (int i = 1; i <= 5; i++) {
        System.out.println(file.readLine());
    }
    current = file.getFilePointer();
    file.seek(current + 6);
    file.write("27".getBytes());
    file.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from ww  w.  j  a va  2 s  .  c o m
        // create a new RandomAccessFile with filename Example
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("java2s.com Hello World");

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

        // read and print the contents of the file
        System.out.println(raf.readUTF());

        // return the file pointer
        System.out.println(raf.getFilePointer());

        // change the position of the file pointer
        raf.seek(5);

        // return the file pointer
        System.out.println(raf.getFilePointer());

        // close the strea and release resources
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("books.dat", "rw");

    String books[] = new String[5];
    books[0] = "A";
    books[1] = "B";
    books[2] = "C";
    books[3] = "D";
    books[4] = "E";

    for (int i = 0; i < books.length; i++) {
        raf.writeUTF(books[i]);/*w ww. ja  va2  s  . c  om*/
    }
    raf.seek(raf.length());
    raf.writeUTF("Servlet & JSP Programming");

    raf.seek(0);

    while (raf.getFilePointer() < raf.length()) {
        System.out.println(raf.readUTF());
    }
}

From source file:MainClass.java

public static void main(String[] argv) throws IOException {
    RandomAccessFile randomAccessFile = new RandomAccessFile("test.dat", "r");

    randomAccessFile.seek(1000);/*from   w  w  w. ja  v a2  s . co m*/

    FileChannel fileChannel = randomAccessFile.getChannel();

    // This will print "1000"
    System.out.println("file pos: " + fileChannel.position());

    randomAccessFile.seek(500);

    // This will print "500"
    System.out.println("file pos: " + fileChannel.position());

    fileChannel.position(200);

    // This will print "200"
    System.out.println("file pos: " + randomAccessFile.getFilePointer());
}

From source file:CreateEmployeeFile.java

public static void main(String[] args) throws Exception {
    String[] fnames = { "A", "B", "C" };

    String[] lnames = { "a", "b", "c" };

    String[] addresses = { "Box 100", "55 Street", "6 Lane" };

    byte[] ages = { 46, 59, 32 };

    double[] salaries = { 5.0, 6.0, 3.0 };

    RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");

    EmployeeRecord er = new EmployeeRecord();

    for (int i = 0; i < fnames.length; i++) {
        er.setFirstName(fnames[i]);/*from  w  ww  .j  a  va2 s  .c o m*/
        er.setLastName(lnames[i]);
        er.setAddress(addresses[i]);
        er.setAge(ages[i]);
        er.setSalary(salaries[i]);
        er.write(raf);
    }
    raf = new RandomAccessFile("employee.dat", "rw");

    er = new EmployeeRecord();

    int numRecords = (int) raf.length() / er.size();

    for (int i = 0; i < numRecords; i++) {
        er.read(raf);

        System.out.print(er.getFirstName() + " ");
        System.out.print(er.getLastName() + " ");
        System.out.print(er.getAddress() + " ");
        System.out.print(er.getAge() + " ");
        System.out.println(er.getSalary());
    }
    raf.seek(0);
    for (int i = 0; i < numRecords; i++) {
        er.read(raf);
        if (er.getAge() >= 55) {
            er.setSalary(0.0);
            raf.seek(raf.getFilePointer() - er.size());
            er.write(raf);
            raf.seek(raf.getFilePointer() - er.size());
            er.read(raf);
        }
        System.out.print(er.getFirstName() + " ");
        System.out.print(er.getLastName() + " ");
        System.out.print(er.getAddress() + " ");
        System.out.print(er.getAge() + " ");
        System.out.println(er.getSalary());
    }

}

From source file:Main.java

public static void incrementReadCounter(RandomAccessFile raf) throws IOException {
    long currentPosition = raf.getFilePointer();
    raf.seek(0);/*from   www .  j a  v a2s. co  m*/
    int counter = raf.readInt();
    counter++;
    raf.seek(0);
    raf.writeInt(counter);
    raf.seek(currentPosition);
}

From source file:dk.netarkivet.common.utils.cdx.BinSearch.java

/** Skip to the next line after the given position by
 * reading a line.  Note that if the position is at the start
 * of a line, it will go to the next line.
 *
 * @param in A file to read from/*w w  w .ja  v  a2s .  c o m*/
 * @param pos The position to start at.
 * @return A new position in the file.  The file's pointer (as given by
 * getFilePointer()) is updated to match.
 * @throws IOException If some I/O error occurs
 */
private static long skipToLine(RandomAccessFile in, long pos) throws IOException {
    in.seek(pos);
    in.readLine();
    return in.getFilePointer();
}

From source file:org.mycontroller.standalone.api.jaxrs.utils.McServerLogFile.java

public static LogFileJson getLogUpdate(Long lastKnownPosition, Long lastNPosition) {
    if (lastNPosition != null && appLogFile.length() > lastNPosition) {
        lastKnownPosition = appLogFile.length() - lastNPosition;
    } else if (lastKnownPosition != null && appLogFile.length() <= lastKnownPosition) {
        return LogFileJson.builder().lastKnownPosition(lastKnownPosition).build();
    }/*from  ww  w .  j  av  a  2s . c  o  m*/
    if (lastKnownPosition == null) {
        lastKnownPosition = 0l;
    }

    //Set maximum limit
    if ((appLogFile.length() - lastKnownPosition) > MAX_POSITION_LIMIT) {
        lastKnownPosition = appLogFile.length() - MAX_POSITION_LIMIT;
    }

    logBuilder.setLength(0);
    // Reading and writing file
    RandomAccessFile readFileAccess = null;
    try {
        readFileAccess = new RandomAccessFile(appLogFile, "r");
        readFileAccess.seek(lastKnownPosition);
        String log = null;
        while ((log = readFileAccess.readLine()) != null) {
            logBuilder.append(log).append("\n");
        }
        lastKnownPosition = readFileAccess.getFilePointer();
    } catch (FileNotFoundException ex) {
        _logger.error("Error,", ex);
    } catch (IOException ex) {
        _logger.error("Error,", ex);
    } finally {
        if (readFileAccess != null) {
            try {
                readFileAccess.close();
            } catch (IOException ex) {
                _logger.error("Error,", ex);
            }
        }
    }
    return LogFileJson.builder().lastKnownPosition(lastKnownPosition).data(logBuilder.toString()).build();
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static long checksum(RandomAccessFile raFile, int numBytes)
        throws IOException, InsufficientFileLengthException {
    CRC32 _crc = new CRC32();

    long pos = raFile.getFilePointer();
    try {// w  w  w  .  j a  v a 2  s .c  o  m
        byte[] buffer = new byte[numBytes];
        raFile.seek(0);
        int n = raFile.read(buffer);
        if (n < numBytes) {
            String s;
            logger.warn(s = ("not enough data for checksum: current file size=" + n));
            throw new InsufficientFileLengthException(s);
        }

        synchronized (_crc) {
            _crc.reset();
            _crc.update(buffer);

            return _crc.getValue();
        }
    } finally {
        raFile.seek(pos);
    }

}

From source file:org.mycontroller.standalone.utils.McServerFileUtils.java

public static LogFile getLogUpdate(Long lastKnownPosition, Long lastNPosition) {
    if (lastNPosition != null && appLogFile.length() > lastNPosition) {
        lastKnownPosition = appLogFile.length() - lastNPosition;
    } else if (lastKnownPosition != null && appLogFile.length() <= lastKnownPosition) {
        return LogFile.builder().lastKnownPosition(lastKnownPosition).build();
    }//w  ww  .j  a va 2 s .c o m
    if (lastKnownPosition == null) {
        lastKnownPosition = 0L;
    }

    //Set maximum limit
    if ((appLogFile.length() - lastKnownPosition) > MAX_POSITION_LIMIT) {
        lastKnownPosition = appLogFile.length() - MAX_POSITION_LIMIT;
    }

    logBuilder.setLength(0);
    // Reading and writing file
    RandomAccessFile readFileAccess = null;
    try {
        readFileAccess = new RandomAccessFile(appLogFile, "r");
        readFileAccess.seek(lastKnownPosition);
        String log = null;
        while ((log = readFileAccess.readLine()) != null) {
            logBuilder.append(log).append("\n");
        }
        lastKnownPosition = readFileAccess.getFilePointer();
    } catch (FileNotFoundException ex) {
        _logger.error("Error,", ex);
    } catch (IOException ex) {
        _logger.error("Error,", ex);
    } finally {
        if (readFileAccess != null) {
            try {
                readFileAccess.close();
            } catch (IOException ex) {
                _logger.error("Error,", ex);
            }
        }
    }
    return LogFile.builder().lastKnownPosition(lastKnownPosition).data(logBuilder.toString()).build();
}