Example usage for java.io ObjectInputStream readChar

List of usage examples for java.io ObjectInputStream readChar

Introduction

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

Prototype

public char readChar() throws IOException 

Source Link

Document

Reads 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);/*from  www  .j a va2s.com*/
    oout.writeChar('A');
    oout.flush();
    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: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  w  w.  java2  s  . co 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

/**
 * Reads a <code>AttributedString</code> object that has been serialised by
 * the {@link SerialUtilities#writeAttributedString(AttributedString,
 * ObjectOutputStream)} method.//from   w  ww. jav a 2  s .  c om
 *
 * @param stream  the input stream (<code>null</code> not permitted).
 *
 * @return The attributed string object (possibly <code>null</code>).
 *
 * @throws IOException  if there is an I/O problem.
 * @throws ClassNotFoundException  if there is a problem loading a class.
 */
public static AttributedString readAttributedString(ObjectInputStream stream)
        throws IOException, ClassNotFoundException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    AttributedString result = null;
    final boolean isNull = stream.readBoolean();
    if (!isNull) {
        // read string and attributes then create result
        String plainStr = (String) stream.readObject();
        result = new AttributedString(plainStr);
        char c = stream.readChar();
        int start = 0;
        while (c != CharacterIterator.DONE) {
            int limit = stream.readInt();
            Map atts = (Map) stream.readObject();
            result.addAttributes(atts, start, limit);
            start = limit;
            c = stream.readChar();
        }
    }
    return result;
}

From source file:CharArrayList.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();/* ww  w .j a  v a 2 s .c  o m*/
    array = new char[in.readInt()];
    for (int i = 0; i < size; i++) {
        array[i] = in.readChar();
    }
}

From source file:org.datanucleus.store.hbase.fieldmanager.FetchFieldManager.java

private char fetchCharInternal(AbstractMemberMetaData mmd, byte[] bytes) {
    char value;//  w w  w .j  av  a2s .com
    if (bytes == null) {
        // Handle missing field
        String dflt = HBaseUtils.getDefaultValueForMember(mmd);
        if (dflt != null && dflt.length() > 0) {
            return dflt.charAt(0);
        }
        return 0;
    }

    if (mmd.isSerialized()) {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            value = ois.readChar();
            ois.close();
            bis.close();
        } catch (IOException e) {
            throw new NucleusException(e.getMessage(), e);
        }
    } else {
        String strValue = new String(bytes, Charsets.UTF_8);
        value = strValue.charAt(0);
    }
    return value;
}

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

/**
 * Called while deserializing a temporal proposition.
 * //from w  ww  .  j ava  2  s  .  c om
 * @param s
 *            an {@link ObjectInputStream}.
 * @throws IOException
 *             input/output error during deserialization.
 * @throws ClassNotFoundException
 *             class of a serialized object cannot be found.
 */
protected void readTemporalProposition(ObjectInputStream s) throws IOException, ClassNotFoundException {
    int mode = s.readChar();
    try {
        switch (mode) {
        case 0:
            setInterval(INTERVAL_FACTORY.getInstance(s.readLong(), (Granularity) s.readObject()));
            break;
        case 1:
            setInterval(INTERVAL_FACTORY.getInstance(s.readLong(), (Granularity) s.readObject(), s.readLong(),
                    (Granularity) s.readObject()));
            break;
        case 2:
            setInterval(INTERVAL_FACTORY.getInstance((Long) s.readObject(), (Long) s.readObject(),
                    (Granularity) s.readObject(), (Long) s.readObject(), (Long) s.readObject(),
                    (Granularity) s.readObject()));
            break;
        default:
            throw new InvalidObjectException("Can't restore. Invalid mode: " + mode);
        }
    } catch (IllegalArgumentException iae) {
        throw new InvalidObjectException("Can't restore: " + iae.getMessage());
    }
}