Stream Converter Unicode : Code Unicode « 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
Microsoft Office Word 2007 Tutorial
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
Java » Development Class » Code UnicodeScreenshots 
Stream Converter Unicode
Stream Converter Unicode

/* From http://java.sun.com/docs/books/tutorial/index.html */
/*
 * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby granted
 * provided that this copyright notice appears in all copies. Please refer to
 * the file "copyright.html" for further important copyright and licensing
 * information.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class StreamConverter {

  static void writeOutput(String str) {

    try {
      FileOutputStream fos = new FileOutputStream("test.txt");
      Writer out = new OutputStreamWriter(fos, "UTF8");
      out.write(str);
      out.close();
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  static String readInput() {

    StringBuffer buffer = new StringBuffer();
    try {
      FileInputStream fis = new FileInputStream("test.txt");
      InputStreamReader isr = new InputStreamReader(fis, "UTF8");
      Reader in = new BufferedReader(isr);
      int ch;
      while ((ch = in.read()) > -1) {
        buffer.append((charch);
      }
      in.close();
      return buffer.toString();
    catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static void main(String[] args) {

    String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");

    writeOutput(jaString);
    String inputString = readInput();
    String displayString = jaString + " " + inputString;
    new ShowString(displayString, "Conversion Demo");
  }

}

class ShowString extends Frame {

  FontMetrics fontM;

  String outString;

  ShowString(String target, String title) {

    setTitle(title);
    outString = target;

    Font font = new Font("Monospaced", Font.PLAIN, 36);
    fontM = getFontMetrics(font);
    setFont(font);

    int size = 0;
    for (int i = 0; i < outString.length(); i++) {
      size += fontM.charWidth(outString.charAt(i));
    }
    size += 24;

    setSize(size, fontM.getHeight() 60);
    setLocation(getSize().width / 2, getSize().height / 2);
    show();
  }

  public void paint(Graphics g) {
    Insets insets = getInsets();
    int x = insets.left;
    int y = insets.top;
    g.drawString(outString, x + 6, y + fontM.getAscent() 14);
  }
}



           
       
Related examples in the same category
1. Unicode sortingUnicode sorting
2. Unicode: Fonts and Text RenderingUnicode: Fonts and Text Rendering
3. Unicode: TrueType Font TestUnicode: TrueType Font Test
4. Unicode: test layoutUnicode: test layout
5. Conversion between Unicode characters and StringsConversion between Unicode characters and Strings
6. Convert among Unicode, ASCII and byte/intConvert among Unicode, ASCII and byte/int
7. Unicode - show a page of Unicode characters
8. Vis - make special characters visible
9. Demonstrate creating readers and writers with a specific encoding
10. TextArea with UnicodeTextArea with Unicode
11. Unicode displayUnicode display
12. Internationalized Graphical User Interfaces: unicode cut and pasteInternationalized Graphical User Interfaces: unicode cut and paste
13. String Converter UnicodeString Converter Unicode
www_.___j_a___va_2s.__co__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.