Example usage for java.io RandomAccessFile writeByte

List of usage examples for java.io RandomAccessFile writeByte

Introduction

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

Prototype

public final void writeByte(int v) throws IOException 

Source Link

Document

Writes a byte to the file as a one-byte value.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  ww  .ja  va 2s.  c o  m*/
        byte[] b1 = { 15, 8 };

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

        raf.writeByte(b1[0]);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeByte(b1[1]);

        raf.seek(0);

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

}

From source file:CreateEmployeeFile.java

void write(RandomAccessFile raf) throws IOException {
    StringBuffer sb;//  w w w  . j a  v  a  2 s  .  c o  m
    if (lastName != null)
        sb = new StringBuffer(lastName);
    else
        sb = new StringBuffer();

    sb.setLength(15);
    raf.writeChars(sb.toString());

    if (firstName != null)
        sb = new StringBuffer(firstName);
    else
        sb = new StringBuffer();

    sb.setLength(15);
    raf.writeChars(sb.toString());

    if (address != null)
        sb = new StringBuffer(address);
    else
        sb = new StringBuffer();

    sb.setLength(30);
    raf.writeChars(sb.toString());
    raf.writeByte(age);
    raf.writeDouble(salary);
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java

/**
 * Corrupt the byte at the given offset in the given file,
 * by subtracting 1 from it./*from  w  w  w. j a  va 2  s.c om*/
 */
private void corruptByteInFile(File file, long offset) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    try {
        raf.seek(offset);
        int origByte = raf.read();
        raf.seek(offset);
        raf.writeByte(origByte - 1);
    } finally {
        IOUtils.closeStream(raf);
    }
}

From source file:org.commoncrawl.service.listcrawler.CrawlList.java

void writeInitialSubDomainMetadataToDisk() throws IOException {

    RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw");

    try {/*from w ww . j a  va2 s . c om*/

        file.writeByte(0); // version
        file.writeInt(_transientSubDomainStats.size());

        ArrayList<CrawlListMetadata> sortedMetadata = new ArrayList<CrawlListMetadata>();
        sortedMetadata.addAll(_transientSubDomainStats.values());
        _transientSubDomainStats = null;
        CrawlListMetadata metadataArray[] = sortedMetadata.toArray(new CrawlListMetadata[0]);
        Arrays.sort(metadataArray, new Comparator<CrawlListMetadata>() {

            @Override
            public int compare(CrawlListMetadata o1, CrawlListMetadata o2) {
                int result = ((Integer) o2.getUrlCount()).compareTo(o1.getUrlCount());
                if (result == 0) {
                    result = o1.getDomainName().compareTo(o2.getDomainName());
                }
                return result;
            }
        });

        DataOutputBuffer outputBuffer = new DataOutputBuffer(CrawlListMetadata.Constants.FixedDataSize);

        TreeMap<Long, Integer> idToOffsetMap = new TreeMap<Long, Integer>();

        for (CrawlListMetadata entry : metadataArray) {
            // reset output buffer 
            outputBuffer.reset();
            // write item to disk 
            entry.serialize(outputBuffer, new BinaryProtocol());

            if (outputBuffer.getLength() > CrawlListMetadata.Constants.FixedDataSize) {
                LOG.fatal("Metadata Serialization for List:" + getListId() + " SubDomain:"
                        + entry.getDomainName());
                System.out.println("Metadata Serialization for List:" + getListId() + " SubDomain:"
                        + entry.getDomainName());
            }
            // save offset 
            idToOffsetMap.put(entry.getDomainHash(), (int) file.getFilePointer());
            // write out fixed data size 
            file.write(outputBuffer.getData(), 0, CrawlListMetadata.Constants.FixedDataSize);
        }

        // write lookup table 
        _offsetLookupTable = new DataOutputBuffer(idToOffsetMap.size() * OFFSET_TABLE_ENTRY_SIZE);

        for (Map.Entry<Long, Integer> entry : idToOffsetMap.entrySet()) {
            _offsetLookupTable.writeLong(entry.getKey());
            _offsetLookupTable.writeInt(entry.getValue());
        }
    } finally {
        file.close();
    }
    _transientSubDomainStats = null;
}

From source file:org.stem.db.FatFileAllocator.java

public static void allocateFile(String filePath, long sizeInMB, boolean mark) throws IOException {
    long started = System.currentTimeMillis();

    Closer closer = Closer.create();/*from   w  ww  . j av a2  s . c o m*/
    try {
        File file = new File(filePath);
        if (file.exists())
            throw new IOException(String.format("File already exists: %s", filePath));

        RandomAccessFile rw = closer.register(new RandomAccessFile(file, "rw"));
        rw.setLength(sizeInMB * 1024 * 1024);
        if (mark) {
            rw.seek(0);
            rw.writeByte(FatFile.MARKER_BLANK);
            rw.seek(rw.length() - 1);
            rw.writeByte(FatFile.MARKER_BLANK);
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
        logger.debug("{} was allocated in {} ms", filePath, System.currentTimeMillis() - started);
    }
}