Created
April 4, 2019 22:28
-
-
Save Bill/73c8f7d92a10902ffa537d9f15f13e13 to your computer and use it in GitHub Desktop.
Creates an EnumSet with a couple elements and prints it in String and OutputStream format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
outputs: