JAVA Internalization Tutorial
Selected Reading
- Java I18N - Discussion
- Java I18N - Useful Resources
- Java I18N - Quick Guide
- JAVA I18N - From Reader and To Writer Conversion
- JAVA I18N - From and To String Conversion
- JAVA I18N - UTC
- JAVA I18N - Date Format Patterns
- JAVA I18N - DateFormatSymbols Class
- JAVA I18N - Formatting Date
- JAVA I18N - SimpleDateFormat Class
- JAVA I18N - Formatting Date and Time
- JAVA I18N - Formatting Time
- JAVA I18N - Formatting Dates
- JAVA I18N - DateFormat Class
- JAVA I18N - Grouping Digits
- JAVA I18N - DecimalFormatSymbols Class
- JAVA I18N - Locale Specific DecimalFormat
- JAVA I18N - Formatting Patterns
- JAVA I18N - DecimalFormat Class
- JAVA I18N - Parsing Numbers
- JAVA I18N - Set Rounding Mode
- JAVA I18N - Set Min/Max Precision
- JAVA I18N - Format Percentages
- JAVA I18N - Format Currencies
- JAVA I18N - NumberFormat Class
- JAVA I18N - ResourceBundle Class
- JAVA I18N - Display Language
- JAVA I18N - Locale Details
- JAVA I18N - Locale Class
- JAVA I18N - Environment Setup
- JAVA I18N - Overview
- JAVA I18N - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JAVA I18N - From Reader and To Writer Conversion
Java Internapzation - Unicode Conversion from/to Reader/Writer
Reader and Writer classes are character oriented stream classes. These can be used to read and convert Unicode characters.
Conversion
Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[] using Reader and Writer classes.
IOTester.java
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; import java.text.ParseException; pubpc class I18NTester { pubpc static void main(String[] args) throws ParseException, IOException { String input = "This is a sample text" ; InputStream inputStream = new ByteArrayInputStream(input.getBytes()); //get the UTF-8 data Reader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); //convert UTF-8 to Unicode int data = reader.read(); while(data != -1){ char theChar = (char) data; System.out.print(theChar); data = reader.read(); } reader.close(); System.out.println(); //Convert Unicode to UTF-8 Bytes ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8")); writer.write(input); writer.close(); String out = new String(outputStream.toByteArray()); System.out.println(out); } }
Output
It will print the following result.
This is a sample text This is a sample text