Example usage for java.io RandomAccessFile readChar

List of usage examples for java.io RandomAccessFile readChar

Introduction

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

Prototype

public final char readChar() throws IOException 

Source Link

Document

Reads a character from this file.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("filename");
    RandomAccessFile raf = new RandomAccessFile(f, "rw");

    // Read a character
    char ch = raf.readChar();

    // Seek to end of file
    raf.seek(f.length());/*from ww w.  j  a v  a 2  s.c om*/

    // Append to the end
    raf.writeChars("aString");
    raf.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {// www .j  ava2s .  c o  m
        int i = 70;

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

        raf.writeChar(i);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeChar(71);

        raf.seek(0);

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

}

From source file:Main.java

public static void main(String[] args) {
    try {/*  w  w w .  j a v  a  2s  .  c o m*/
        String s = "Hello World from java2s.com";

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

        raf.writeChars(s);

        raf.seek(0);

        for (int i = 0; i < 15; i++) {
            System.out.println(raf.readChar());
        }

        raf.seek(0);

        raf.writeChars("This is an example from java2s.com");

        raf.seek(0);

        for (int i = 0; i < 20; i++) {
            System.out.println(raf.readChar());
        }
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:RandomIOApp.java

public static void main(String args[]) throws IOException {
    RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
    file.writeBoolean(true);// w w  w .  j a  va  2 s . c o  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:Main.java

public static void main(String[] args) {
    try {/*  www.  j  a  v  a  2  s.  c  o  m*/
        char c = 'J';

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

        // write something in the file
        raf.writeChar('A');

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

        // read char
        System.out.println(raf.readChar());

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

        // write a char at the start
        raf.writeChar(c);

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

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

}

From source file:CreateEmployeeFile.java

void read(RandomAccessFile raf) throws IOException {
    char[] temp = new char[15];
    for (int i = 0; i < temp.length; i++)
        temp[i] = raf.readChar();
    lastName = new String(temp);
    temp = new char[15];
    for (int i = 0; i < temp.length; i++)
        temp[i] = raf.readChar();/*from  w  w w.  j  a  v a2  s .  co  m*/
    firstName = new String(temp);
    temp = new char[30];
    for (int i = 0; i < temp.length; i++)
        temp[i] = raf.readChar();

    address = new String(temp);
    age = raf.readByte();
    salary = raf.readDouble();
}

From source file:big.BigZip.java

/**
 * Version 2 that permits to extract the text from a compressed file without
 * creating any file on the disk./*  www .j ava2s .  co  m*/
 * @param filePosition
 * @return      The source code of the compressed file
 */
public String extractBytesToRAM(final long filePosition) {

    String result = null;

    try {

        // add the signature bytes to our start position
        long startPosition = filePosition + magicSignature.length();

        // enable random access to the BIG file (fast as heck)
        RandomAccessFile dataBIG = new RandomAccessFile(fileMainBIG, "r");
        // jump directly to the position where the file is positioned
        dataBIG.seek(startPosition);
        // create a byte array
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();

        // get the end of this file entry (by brute-force)
        char test = 0;
        long endPosition = -1;
        while (test != -1) {
            test = dataBIG.readChar();
            // if the magic devil number was found..
            if (test == 66) {
                // read the next value for confirmation
                byte value = dataBIG.readByte();
                if (value != 73) {
                    continue;
                }
                // we found the next entry
                endPosition = dataBIG.getFilePointer() - 1;
                break;
            }
        }

        // rewind back to the start position
        dataBIG.seek(startPosition);

        // now we start reading bytes during the mentioned interval
        while (dataBIG.getFilePointer() < endPosition) {
            // read a byte from our BIG archive
            int data = dataBIG.read();
            byteOutput.write(data);
        }
        // flush data at this point
        byteOutput.flush();
        // now convert the stream from input into an output (to feed the zip stream)
        ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
        // where we place the decompressed bytes
        ByteArrayOutputStream textOutput = new ByteArrayOutputStream();
        // create the zip streamer
        final ArchiveInputStream archiveStream;
        archiveStream = new ArchiveStreamFactory().createArchiveInputStream("zip", byteInput);
        final ZipArchiveEntry entry = (ZipArchiveEntry) archiveStream.getNextEntry();
        // copy all bytes from one location to the other (and decompress the data)
        IOUtils.copy(archiveStream, textOutput);
        // flush the results
        textOutput.flush();
        // we've got the result right here!
        result = textOutput.toString();
        // now close all the streams that we have open
        dataBIG.close();
        byteOutput.close();
        byteInput.close();
        textOutput.close();
        archiveStream.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(BigZip.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (IOException ex) {
        Logger.getLogger(BigZip.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (ArchiveException ex) {
        Logger.getLogger(BigZip.class.getName()).log(Level.SEVERE, null, ex);
    }

    return result;
}