Java Code Snippets
>> Thursday, September 25, 2008
Hey! I created this blog to share some java code snippets with you. They all seem to be basic, but i hope these snippets can help your day to day programming.
Encoding a String:
Encoding a String:
Charset charset = Charset.forName("UTF-32");
String text = "Hello";
ByteBuffer encoded = charset.newEncoder().encode(CharBuffer.
wrap(text));
System.out.println("Encoded: " + new String(encoded.array()));
CharBuffer decoded = charset.newDecoder().decode(encoded);
System.out.println("Decoded: " : " + new String(decoded.array()));
Reading/writing contents from/to a file with encoding:
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("EncodingTest.txt"), "UTF-32"));
writer.write("I'm here to help you for reading/writing " +
"contents from/to a file with encoding");
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("EncodingTest.txt"), "UTF-32"));
System.out.println(reader.readLine());
reader.close();
For more available character sets available in java ask "Charset.availableCharsets()".
Read more...