Example usage for java.io ObjectOutputStream writeChar

List of usage examples for java.io ObjectOutputStream writeChar

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeChar.

Prototype

public void writeChar(int val) throws IOException 

Source Link

Document

Writes a 16 bit char.

Usage

From source file:Main.java

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

    char b = 'F';

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeChar(b);
    oout.writeChar('A');
    oout.flush();/*from w  w w.  jav  a  2  s  .  c om*/
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print a char
    System.out.println(ois.readChar());

    // read and print a char
    System.out.println(ois.readChar());
    ois.close();
}

From source file:Main.java

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

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.writeChar(100);
    out.close();/*  ww w  .j  av a  2 s .  c o m*/

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:DataIOTest2.java

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

    // write the data out
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("invoice1.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);/*from  w  ww  .  j a  v a2 s  .c  o m*/
        out.writeChar('\t');
        out.writeInt(units[i]);
        out.writeChar('\t');
        out.writeChars(descs[i]);
        out.writeChar('\n');
    }
    out.close();

    // read it in again
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("invoice1.txt"));

    double price;
    int unit;
    String desc;
    double total = 0.0;

    try {
        while (true) {
            price = in.readDouble();
            in.readChar(); // throws out the tab
            unit = in.readInt();
            in.readChar(); // throws out the tab
            desc = in.readLine();
            System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
            total = total + unit * price;
        }
    } catch (EOFException e) {
    }
    System.out.println("For a TOTAL of: $" + total);
    in.close();
}

From source file:Main.java

/**
 * Serialises an <code>AttributedString</code> object.
 *
 * @param as  the attributed string object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*from ww w  . j a v a2s . co  m*/
public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (as != null) {
        stream.writeBoolean(false);
        AttributedCharacterIterator aci = as.getIterator();
        // build a plain string from aci
        // then write the string
        StringBuffer plainStr = new StringBuffer();
        char current = aci.first();
        while (current != CharacterIterator.DONE) {
            plainStr = plainStr.append(current);
            current = aci.next();
        }
        stream.writeObject(plainStr.toString());

        // then write the attributes and limits for each run
        current = aci.first();
        int begin = aci.getBeginIndex();
        while (current != CharacterIterator.DONE) {
            // write the current character - when the reader sees that this
            // is not CharacterIterator.DONE, it will know to read the
            // run limits and attributes
            stream.writeChar(current);

            // now write the limit, adjusted as if beginIndex is zero
            int limit = aci.getRunLimit();
            stream.writeInt(limit - begin);

            // now write the attribute set
            Map atts = new HashMap(aci.getAttributes());
            stream.writeObject(atts);
            current = aci.setIndex(limit);
        }
        // write a character that signals to the reader that all runs
        // are done...
        stream.writeChar(CharacterIterator.DONE);
    } else {
        // write a flag that indicates a null
        stream.writeBoolean(true);
    }

}

From source file:CharArrayList.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();/*w w w . j  a  v a  2  s .c o  m*/
    out.writeInt(array.length);
    for (int i = 0; i < size; i++) {
        out.writeChar(array[i]);
    }
}

From source file:org.protempa.proposition.TemporalProposition.java

/**
 * Called while serializing a temporal proposition. It optimizes for when
 * the temporal proposition's interval is a {@link SimpleInterval}.
 * /*  ww  w . j  av  a  2s. c om*/
 * @param s
 *            an {@link ObjectOutputStream}.
 * @throws IOException
 *             when an error occurs during serialization.
 */
protected void writeTemporalProposition(ObjectOutputStream s) throws IOException {
    if (this.interval instanceof SimpleInterval) {
        long start = this.interval.getMinStart();
        long finish = this.interval.getMinFinish();
        Granularity startGran = this.interval.getStartGranularity();
        Granularity finishGran = this.interval.getFinishGranularity();
        if (start == finish && startGran == finishGran) {
            s.writeChar(0);
            s.writeLong(start);
            s.writeObject(startGran);
        } else {
            s.writeChar(1);
            s.writeLong(start);
            s.writeObject(startGran);
            s.writeLong(finish);
            s.writeObject(finishGran);
        }
    } else {
        s.writeChar(2);
        s.writeObject(this.interval.getMinStart());
        s.writeObject(this.interval.getMaxStart());
        s.writeObject(this.interval.getStartGranularity());
        s.writeObject(this.interval.getMinFinish());
        s.writeObject(this.interval.getMaxFinish());
        s.writeObject(this.interval.getFinishGranularity());
    }

}