Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates an EnumSet with a couple elements and prints it in String and OutputStream format.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.EnumSet;
class Scratch {
enum Fruit {APPLE, ORANGE};
public static void main(String[] args) throws IOException {
final EnumSet<Fruit> fruits = EnumSet.of(Fruit.APPLE, Fruit.ORANGE);
final String serialized = serializeToString(fruits);
System.out.println(String.format("fruits as String:%n%s%n…as OutputStream (length %d chars):%n%s",
fruits, serialized.length(), serialized));
}
private static String serializeToString(final Serializable s) throws IOException {
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(buffer)) {
oos.writeObject(s);
return buffer.toString();
}
}
}
@Bill

This comment has been minimized.

Copy link
Owner Author

commented Apr 4, 2019

outputs:

fruits as String:
[APPLE, ORANGE]
…as OutputStream (length 248 chars):
Scratch$Fruit�����������xr��java.lang.Enum�����������xpur��[Ljava.lang.Enum;���-3�/����xp����~q�~��t��APPLE~q�~��t��ORANGE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.