Example usage for java.io RandomAccessFile writeUTF

List of usage examples for java.io RandomAccessFile writeUTF

Introduction

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

Prototype

public final void writeUTF(String str) throws IOException 

Source Link

Document

Writes a string to the file using modified UTF-8 encoding in a machine-independent manner.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/* www .j  a v a  2s  .  co 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  .  j a  v a  2  s .co m*/
    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:Main.java

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

From source file:net.iharding.utils.FileUtils.java

/**   
 * RandomAccessFile   /*from   w w  w  .  j  a  v  a  2s . c  o m*/
 *    
 * @param fileName ??   
 * @param content    
 */
public static void appendFile(String filePath, String content) {
    RandomAccessFile randomFile = null;
    try {
        // ???     
        randomFile = new RandomAccessFile(filePath, "rw");
        //      
        long fileLength = randomFile.length();
        //      
        randomFile.seek(fileLength);
        randomFile.writeUTF(content);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (randomFile != null) {
            try {
                randomFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:WordList.java

public static void writeWords(String filename, String[] words) throws IOException {
    // Open the file for read/write access ("rw"). We only need to write,
    // but have to request read access as well
    RandomAccessFile f = new RandomAccessFile(filename, "rw");

    // This array will hold the positions of each word in the file
    long wordPositions[] = new long[words.length];

    // Reserve space at the start of the file for the wordPositions array
    // and the length of that array. 4 bytes for length plus 8 bytes for
    // each long value in the array.
    f.seek(4L + (8 * words.length));/*w ww.  j  av a  2s . c om*/

    // Now, loop through the words and write them out to the file,
    // recording the start position of each word. Note that the
    // text is written in the UTF-8 encoding, which uses 1, 2, or 3 bytes
    // per character, so we can't assume that the string length equals
    // the string size on the disk. Also note that the writeUTF() method
    // records the length of the string so it can be read by readUTF().
    for (int i = 0; i < words.length; i++) {
        wordPositions[i] = f.getFilePointer(); // record file position
        f.writeUTF(words[i]); // write word
    }

    // Now go back to the beginning of the file and write the positions
    f.seek(0L); // Start at beginning
    f.writeInt(wordPositions.length); // Write array length
    for (int i = 0; i < wordPositions.length; i++)
        // Loop through array
        f.writeLong(wordPositions[i]); // Write array element
    f.close(); // Close the file when done.
}

From source file:net.ontopia.infoset.content.FileContentStore.java

private void allocateNewBlock() throws ContentStoreException {
    RandomAccessFile out = null;
    boolean exception_thrown = false;
    try {//  w  ww. j  av a 2  s  .  co  m
        out = new RandomAccessFile(key_file, "rws");

        for (int i = 0; i < MAX_SPINS; i++) {
            // acquire exclusive lock
            FileLock l = out.getChannel().tryLock();

            if (l == null) {
                // wait a little before trying again
                try {
                    Thread.sleep(SPIN_TIMEOUT);
                } catch (InterruptedException e) {
                }
                continue;

            } else {
                try {
                    // allocate new key
                    int old_key;
                    int new_key;
                    String content = null;

                    if (out.length() == 0) {
                        old_key = 0;
                        new_key = old_key + KEY_BLOCK_SIZE;

                    } else {
                        try {
                            content = out.readUTF();
                            old_key = Integer.parseInt(content);
                            new_key = old_key + KEY_BLOCK_SIZE;
                        } catch (NumberFormatException e) {
                            if (content.length() > 100)
                                content = content.substring(0, 100) + "...";
                            throw new ContentStoreException(
                                    "Content store key file corrupted. Contained: '" + content + "'");
                        }
                    }

                    // truncate key file and write out new key
                    out.seek(0);
                    out.writeUTF(Integer.toString(new_key));

                    end_of_key_block = new_key;
                    last_key = old_key;
                    return;
                } finally {
                    // release file lock
                    try {
                        l.release();
                    } catch (Throwable t) {
                        throw new ContentStoreException("Could not release key file lock.", t);
                    }
                }
            }
        }
        throw new ContentStoreException("Block allocation timed out.");

    } catch (ContentStoreException e) {
        exception_thrown = true;
        throw e;

    } catch (Throwable t) {
        exception_thrown = true;
        throw new ContentStoreException(t);

    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                if (!exception_thrown)
                    throw new ContentStoreException("Problems occurred when closing content store.", e);
            }
        }
    }
}

From source file:org.xdi.util.FileUtil.java

/**
 * Writes data in a file on specified position
 * //from  www  .j  a v a2s.c o  m
 * @param filePath
 * @param position
 * @param data
 * @return
 */
public boolean writeToFile(String filePath, long position, String data) {

    try {
        File f = new File(filePath);
        RandomAccessFile raf;
        raf = new RandomAccessFile(f, "rw");
        raf.seek(position);
        StringBuilder dataAfterPostion = new StringBuilder(data);
        while (raf.getFilePointer() < raf.length()) {
            String line = raf.readLine();
            dataAfterPostion.append(line);
        }
        raf.seek(position);
        raf.writeUTF(dataAfterPostion.toString());
        raf.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}