Example usage for java.io ObjectOutputStream writeUTF

List of usage examples for java.io ObjectOutputStream writeUTF

Introduction

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

Prototype

public void writeUTF(String str) throws IOException 

Source Link

Document

Primitive data write of this String in modified UTF-8 format.

Usage

From source file:Main.java

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

    String s = "Hello World from java2s.com";

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

    // write something in the file
    oout.writeUTF(s);
    oout.flush();/*from w ww .ja  va  2  s .co  m*/
    oout.close();
    // create an ObjectInputStream for the file we created before
    Main ois = new Main(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readUTF());

    // get the class for string and print the name
    ObjectStreamClass streamClass = ObjectStreamClass.lookup(Integer.class);
    System.out.println(ois.resolveClass(streamClass).getName());
    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.writeObject(Calendar.getInstance());
    out.writeObject(new BigDecimal("123.123"));
    out.writeInt(1);/* ww  w.j a v  a  2  s .  c  o  m*/
    out.writeUTF("tutorial");
    out.close();

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

    BigDecimal price;
    int unit;
    String desc;

    Calendar date = (Calendar) in.readObject();
    System.out.println(date);

    price = (BigDecimal) in.readObject();
    unit = in.readInt();
    desc = in.readUTF();
    System.out.println(unit);
    System.out.println(desc);
    System.out.println(price);
    in.close();
}

From source file:ObjectStreams.java

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

    ObjectOutputStream out = null;
    try {//from  ww  w.  j av a  2s . c om
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

        out.writeObject(Calendar.getInstance());
        for (int i = 0; i < prices.length; i++) {
            out.writeObject(prices[i]);
            out.writeInt(units[i]);
            out.writeUTF(descs[i]);
        }
    } finally {
        out.close();
    }

    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

        Calendar date = null;
        BigDecimal price;
        int unit;
        String desc;
        BigDecimal total = new BigDecimal(0);

        date = (Calendar) in.readObject();

        System.out.format("On %tA, %<tB %<te, %<tY:%n", date);

        try {
            while (true) {
                price = (BigDecimal) in.readObject();
                unit = in.readInt();
                desc = in.readUTF();
                System.out.format("You ordered %d units of %s at $%.2f%n", unit, desc, price);
                total = total.add(price.multiply(new BigDecimal(unit)));
            }
        } catch (EOFException e) {
        }
        System.out.format("For a TOTAL of: $%.2f%n", total);
    } finally {
        in.close();
    }
}

From source file:org.n52.geoar.newdata.PluginLoader.java

/**
 * Saves the state of plugins to {@link SharedPreferences}.
 *///from ww w.j av  a 2s .  co m
public static void saveState() {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

        // store plugin state version
        objectOutputStream.writeInt(PLUGIN_STATE_VERSION);

        List<InstalledPluginHolder> checkedPlugins = mInstalledPlugins.getCheckedItems();
        objectOutputStream.writeInt(checkedPlugins.size());

        for (InstalledPluginHolder plugin : checkedPlugins) {
            objectOutputStream.writeUTF(plugin.getIdentifier());
            plugin.saveState(objectOutputStream);
        }

        SharedPreferences preferences = GeoARApplication.applicationContext
                .getSharedPreferences(GeoARApplication.PREFERENCES_FILE, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        objectOutputStream.flush();
        editor.putString(PLUGIN_STATE_PREF, Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT));
        editor.commit();
        objectOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        // TODO
    }
}

From source file:edu.vt.middleware.webflow.ClientFlowExecutionKey.java

private void writeObject(final ObjectOutputStream out) throws IOException {
    out.writeUTF(toString());
}

From source file:at.molindo.esi4j.rebuild.scrutineer.ObjectIdAndVersion.java

@Override
protected void writeId(ObjectOutputStream objectOutputStream) throws IOException {
    if (_id instanceof String) {
        objectOutputStream.writeBoolean(true);
        objectOutputStream.writeUTF((String) _id);
    } else {/*w w  w .  j  a v a 2s  .  c  o  m*/
        objectOutputStream.writeBoolean(false);
        objectOutputStream.writeLong(((Number) _id).longValue());
    }
}

From source file:it.unibo.alchemist.language.protelis.MethodCall.java

private void writeObject(final ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();/*from   w  ww.  j  a  v a 2s .  co m*/
    out.writeObject(method.getDeclaringClass());
    out.writeUTF(method.getName());
    out.writeObject(method.getParameterTypes());
}

From source file:it.jackbergus.graphdatabase.graph.PropertyGraph.java

private void writeObject(ObjectOutputStream out) throws IOException {
    // custom serialization
    out.writeUTF(relationName);
    core.save();//from  ww  w.j a  va  2  s  .  c om
}

From source file:eu.stratosphere.hadoopcompatibility.mapred.HadoopInputFormat.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeUTF(mapredInputFormat.getClass().getName());
    out.writeUTF(keyClass.getName());/*from  www  .j  a  v a 2 s.com*/
    out.writeUTF(valueClass.getName());
    jobConf.write(out);
}

From source file:eu.stratosphere.hadoopcompatibility.mapreduce.HadoopInputFormat.java

private void writeObject(ObjectOutputStream out) throws IOException {
    out.writeUTF(this.mapreduceInputFormat.getClass().getName());
    out.writeUTF(this.keyClass.getName());
    out.writeUTF(this.valueClass.getName());
    this.configuration.write(out);
}