Example usage for java.io RandomAccessFile length

List of usage examples for java.io RandomAccessFile length

Introduction

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

Prototype

public native long length() throws IOException;

Source Link

Document

Returns the length of this file.

Usage

From source file:ReverseFile.java

public static void main(String args[]) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(args[0], "r");
    long position = raf.length();
    while (position > 0) {
        position -= 1;//from  ww  w.  j  a  v a  2 s. c o  m
        raf.seek(position);
        byte b = raf.readByte();
        System.out.print((char) b);
    }
}

From source file:MainClass.java

public static void main(String args[]) {

    try {//www .  j av  a  2s  .c om

        RandomAccessFile raf = new RandomAccessFile(args[0], "r");

        long position = raf.length();

        while (position > 0) {

            position -= 1;

            raf.seek(position);
            byte b = raf.readByte();

            System.out.print((char) b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:RawDataFromImageFilePDF.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from   w  w  w.  j  av a2 s.c o m*/
        PdfWriter.getInstance(document, new FileOutputStream("RawDataFromImageFilePDF.pdf"));
        document.open();

        RandomAccessFile rf = new RandomAccessFile("logo.png", "r");
        int size = (int) rf.length();
        byte imext[] = new byte[size];
        rf.readFully(imext);
        rf.close();

        Image img1 = Image.getInstance(imext);
        img1.setAbsolutePosition(50, 500);
        document.add(img1);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:Tail.java

public static void main(String args[]) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(args[0], "r");
    long count = 10;
    long position = raf.length();
    position -= count;//from w  w w.  j  a  va  2s .  c  o  m
    if (position < 0)
        position = 0;
    raf.seek(position);
    while (true) {
        try {
            byte b = raf.readByte();
            System.out.print((char) b);
        } catch (EOFException eofe) {
            break;
        }
    }
}

From source file:RawDataFromImageFileAndManipulationPDF.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from  w  w  w .  j a  va  2s.com*/
        PdfWriter.getInstance(document, new FileOutputStream("RawDataFromImageFileAndManipulationPDF.pdf"));
        document.open();

        RandomAccessFile rf = new RandomAccessFile("logo.png", "r");
        int size = (int) rf.length();
        byte imext[] = new byte[size];
        rf.readFully(imext);
        rf.close();

        for (int i = 0; i < imext.length; i++) {
            imext[i] = (byte) (imext[i] + 3);
        }

        Image img1 = Image.getInstance(100, 100, 3, 8, imext);
        img1.setAbsolutePosition(200, 200);
        document.add(img1);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:Main.java

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

    RandomAccessFile raf = new RandomAccessFile("a.dat", "rw");
    int x, y;/*w w w. j a va2  s. c  om*/

    for (long i = 0, j = raf.length() - 1; i < j; i++, j--) {
        raf.seek(i);
        x = raf.read();
        raf.seek(j);
        y = raf.read();

        raf.seek(j);
        raf.write(x);
        raf.seek(i);
        raf.write(y);

    }
    raf.close();
}

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);/*from  ww w  .  jav  a 2s  .c o m*/
    for (int i = 0; i < 10; i++) {
        raf.seek(raf.length() - 4);
        raf.writeInt(raf.readInt());
    }
    raf.close();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    RandomAccessFile fh1 = new RandomAccessFile("a.txt", "r");
    RandomAccessFile fh2 = new RandomAccessFile("b.txt", "r");
    long filesize1 = fh1.length();
    long filesize2 = fh2.length();
    // allocate two buffers large enough to hold entire files
    int bufsize = (int) Math.min(filesize1, filesize2);
    byte[] buffer1 = new byte[bufsize];
    byte[] buffer2 = new byte[bufsize];

    fh1.readFully(buffer1, 0, bufsize);/*  ww  w. j  ava2s.c  o  m*/
    fh2.readFully(buffer2, 0, bufsize);

    for (int i = 0; i < bufsize; i++) {
        if (buffer1[i] != buffer2[i]) {
            System.out.println("Files differ at offset " + i);
            break;
        }
    }
    fh1.close();
    fh2.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    RandomAccessFile f = new RandomAccessFile("hello.txt", "rw");
    FileChannel fc = f.getChannel();
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, f.length());

    int len = (int) f.length();
    for (int i = 0, j = len - 1; i < j; i++, j--) {
        byte b = mbb.get(i);
        mbb.put(i, mbb.get(j));/*  w w  w  . j a  va  2s  .  c  o m*/
        mbb.put(j, b);
    }
    fc.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    if (args.length < 2) {
        System.err.println("Usage: java MainClass inFile1 inFile2... outFile");
        return;/*from   ww w  .j a v a  2 s  .  c  o  m*/
    }

    ByteBuffer[] buffers = new ByteBuffer[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        RandomAccessFile raf = new RandomAccessFile(args[i], "r");
        FileChannel channel = raf.getChannel();
        buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
    }

    FileOutputStream outFile = new FileOutputStream(args[args.length - 1]);
    FileChannel out = outFile.getChannel();
    out.write(buffers);
    out.close();
}