Example usage for java.io RandomAccessFile writeChars

List of usage examples for java.io RandomAccessFile writeChars

Introduction

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

Prototype

public final void writeChars(String s) throws IOException 

Source Link

Document

Writes a string to the file as a sequence of characters.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w w  . ja  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: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());/*  w w  w  . j  a va 2  s  .c om*/

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

From source file:com.izforge.izpack.util.SelfModifier.java

public static void test(String[] args) {
    // open a File for random access in the sandbox, which will cause
    // deletion//from   ww  w. j a  v a2s .  c o m
    // of the file and its parent directories to fail until it is closed (by
    // virtue of this java process halting)
    try {
        File sandbox = new File(System.getProperty(BASE_KEY) + ".d");
        File randFile = new File(sandbox, "RandomAccess.tmp");
        RandomAccessFile rand = new RandomAccessFile(randFile, "rw");
        rand.writeChars("Just a test: The JVM has to close 'cuz I won't!\n");

        System.err.print("Deleting sandbox: ");
        FileUtils.deleteDirectory(sandbox);
        System.err.println(sandbox.exists() ? "FAILED" : "SUCCEEDED");
    } catch (Exception x) {
        System.err.println(x.getMessage());
        x.printStackTrace();
    }
}

From source file:CreateEmployeeFile.java

void write(RandomAccessFile raf) throws IOException {
    StringBuffer sb;//w w w. j  a va  2  s. com
    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);
}