Convert Encoding : I18N « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » I18NScreenshots 
Convert Encoding

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/** A program to convert from one character encoding to another */
public class ConvertEncoding {
  public static void main(String[] args) {
    String from = null, to = null;
    String infile = null, outfile = null;
    for (int i = 0; i < args.length; i++) { // Parse command-line arguments.
      if (i == args.length - 1)
        usage()// All args require another.
      if (args[i].equals("-from"))
        from = args[++i];
      else if (args[i].equals("-to"))
        to = args[++i];
      else if (args[i].equals("-in"))
        infile = args[++i];
      else if (args[i].equals("-out"))
        outfile = args[++i];
      else
        usage();
    }

    try {
      convert(infile, outfile, from, to);
    // Attempt conversion.
    catch (Exception e) { // Handle exceptions.
      System.exit(1);
    }
  }

  public static void usage() {
    System.err.println("Usage: java ConvertEncoding <options>\n"
        "Options:\n\t-from <encoding>\n\t" "-to <encoding>\n\t"
        "-in <file>\n\t-out <file>");
    System.exit(1);
  }

  public static void convert(String infile, String outfile, String from,
      String tothrows IOException, UnsupportedEncodingException {
    // Set up byte streams.
    InputStream in;
    if (infile != null)
      in = new FileInputStream(infile);
    else
      in = System.in;
    OutputStream out;
    if (outfile != null)
      out = new FileOutputStream(outfile);
    else
      out = System.out;

    // Use default encoding if no encoding is specified.
    if (from == null)
      from = System.getProperty("file.encoding");
    if (to == null)
      to = System.getProperty("file.encoding");

    // Set up character streams.
    Reader r = new BufferedReader(new InputStreamReader(in, from));
    Writer w = new BufferedWriter(new OutputStreamWriter(out, to));

    // Copy characters from input to output. The InputStreamReader
    // converts from the input encoding to Unicode, and the
    // OutputStreamWriter converts from Unicode to the output encoding.
    // Characters that cannot be represented in the output encoding are
    // output as '?'
    char[] buffer = new char[4096];
    int len;
    while ((len = r.read(buffer)) != -1)
      // Read a block of input.
      w.write(buffer, 0, len)// And write it out.
    r.close()// Close the input.
    w.close()// Flush and close output.
  }
}

           
       
Related examples in the same category
1. I18N SortI18N Sort
2. Java I18N: Format : Choice Format DemoJava I18N: Format : Choice Format Demo
3. Java I18N: Format : Date FormatJava I18N: Format : Date Format
4. Java I18N: Format : Date Format Symbols DemoJava I18N: Format : Date Format Symbols Demo
5. Java I18N: Format : Message Format DemoJava I18N: Format : Message Format Demo
6. Java I18N: Format : Number FormatJava I18N: Format : Number Format
7. Java I18N: Format : Simple Date FormatJava I18N: Format : Simple Date Format
8. Java I18N: IntroductionJava I18N: Introduction
9. List all LocaleList all Locale
10. Displays Charsets and aliasesDisplays Charsets and aliases
11. Encode and DecodeEncode and Decode
12. List Locale OrientationList Locale Orientation
13. Resource Bundle in Java code
14. List Resource Bundle Creator
15. Simple Resource Bundle
16. Your own Resource Bundle
17. Displaying Formatted Numbers for Alternate LocalesDisplaying Formatted Numbers for Alternate Locales
18. Menu created from property file
19. Unicode DisplayUnicode Display
20. Demonstrate number and date internationalizationDemonstrate number and date internationalization
21. Set of convenience routines for internationalized code
22. Print the default locale
23. Use some locales choicesUse some locales choices
24. Format some values using the default locale
25. List LocalesList Locales
26. Create one button, internationalizedly
27. Show DateShow Date
28. Change the default locale
29. java.util.Locale and java.text.NumberFormatjava.util.Locale and java.text.NumberFormat
30. Collation Test Collation Test
31. Number Format Test Number Format Test
32. Java Internationalization: load string from properties Java Internationalization: load string from properties
33.  Locales  Locales
34. Locale 2Locale 2
35. Display Language OutputDisplay Language Output
36. Display Name OutputDisplay Name Output
37. Display Variant OutputDisplay Variant Output
38. Constant Locale UsageConstant Locale Usage
39. Country Language CodesCountry Language Codes
40. Isolating Locale-Specific Data with Resource Bundles Isolating Locale-Specific Data with Resource Bundles
41. Property To List Resource Bundle
42. Which Bundle Comes First
43. Calendar Manipulation for I18N (Internationalization)Calendar Manipulation for I18N (Internationalization)
44. Formatting Messages: Arabic DigitFormatting Messages: Arabic Digit
45. Formatting Messages: Change EraFormatting Messages: Change Era
46. Formatting Messages: Message Format ReuseFormatting Messages: Message Format Reuse
47. Character Sets and Unicode: Code Set Conversion
48. Searching, Sorting, and Text Boundary Detection: Collation IssuesSearching, Sorting, and Text Boundary Detection: Collation Issues
49. Searching, Sorting, and Text Boundary Detection: Detecting Text BoundariesSearching, Sorting, and Text Boundary Detection: Detecting Text Boundaries
50. Spanish SortSpanish Sort
51. Sort With Collation KeysSort With Collation Keys
52. DecompositionDecomposition
53. Formatting Messages: Date and NumberFormatting Messages: Date and Number
54. Internationalized Graphical User Interfaces: Component OrientationInternationalized Graphical User Interfaces: Component Orientation
55. Internationalized Graphical User Interfaces: Component Orientation 1Internationalized Graphical User Interfaces: Component Orientation 1
56. Localized JOptionPane
57. Big Demo for I18N
58. Component Orientation Bundle
59. Java Input Method Framework
60. Popup in FrenchPopup in French
61. I18N : TextI18N : Text
www.__j_ava_2___s__.__c__o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.