HelloUnicode.zip

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 15
    下载次数
  • 2005-09-06 18:02
    上传日期
Unicode 的介绍,分几个小的例子来说明。希望采纳。
HelloUnicode.zip
  • HelloUnicode.java
    5.9KB
  • www.pudn.com.txt
    218B
内容介绍
/* * Copyright (c) 2002-2003 Che, Dong Email: chedongATbigfoot.com/chedongATchedong.com * $Id: HelloUnicode.java,v 1.3 2003/03/09 08:41:46 chedong Exp $ */ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; /** * 目的: * 测试不同字符编码解码方式对多字节编码(中文)处理的影响 * 输入: * 可以从命令行输入测试字符串 * 输出: * 测试1 按照不同解码方式处理字符串,并按不同编码方式写入文件 * 测试2 按照不同解码方式从文件中将字符串读出 * @author Che, Dong */ class HelloUnicode { /** * main entrance * @param args command line arguments */ public static void main(String[] args) { String hello = "Hello world 世界你好"; //read from command line input if (args.length > 0) { hello = args[0]; } try { /* * 试验1: 从测试字符串按系统缺省编码方式解码,并写入文件 */ System.out.println(">>>>testing1: write hello world to files<<<<"); System.out.println("[test 1-1]: with system default encoding=" + System.getProperty("file.encoding") + "\nstring=" + hello + "\tlength=" + hello.length()); printCharArray(hello); writeFile("hello.orig.html", hello); //把字符串按GB2312解码 hello = new String(hello.getBytes(), "GB2312"); System.out.println( "[test 1-2]: getBytes with platform default encoding and decoding as gb2312:\nstring=" + hello + "\tlength=" + hello.length()); writeFile("hello.gb2312.html", hello); printCharArray(hello); //把字符串按UTF8解码成字节流,并打印相应的字节 hello = new String(hello.getBytes("UTF8")); System.out.println("[test 1-3]: convert string to UTF8\nstring=" + hello + "\tlength=" + hello.length()); writeFile("hello.utf8.html", hello); printCharArray(hello); /* * 试验2: 从试验1的输出文件中读取,并按照不同方式解码 */ System.out.println("=============================================="); System.out.println( ">>>>testing2: reading and decoding from files<<<<"); //first file: encoding with system default hello = readFile("hello.orig.html"); System.out.println( "[test 2-1]: read hello.orig.html: decoding with system default encoding\nstring=" + hello + "\tlength=" + hello.length()); printCharArray(hello); //second file: decoding from GBK hello = readFile("hello.gb2312.html"); hello = new String(hello.getBytes(), "GB2312"); System.out.println( "[test 2-2]: read hello.gb2312.html: decoding as GB2312\nstring=" + hello + "\tlength=" + hello.length()); printCharArray(hello); //third file: decoding from UTF8 hello = readFile("hello.utf8.html"); hello = new String(hello.getBytes(), "UTF8"); System.out.println( "[test 2-3]: read hello.utf8.html: decoding as UTF8\nstring=" + hello + "\tlength=" + hello.length()); printCharArray(hello); } catch (Exception e) { System.out.println(e.toString()); } } /** * print char array * @param inStr input string */ public static void printCharArray(String inStr) { char[] myBuffer = inStr.toCharArray(); //list each Charactor in byte value, short value, and UnicodeBlock Mapping for (int i = 0; i < inStr.length(); i++) { byte b = (byte) myBuffer[i]; short s = (short) myBuffer[i]; String hexB = Integer.toHexString(b).toUpperCase(); String hexS = Integer.toHexString(s).toUpperCase(); StringBuffer sb = new StringBuffer(); //print char sb.append("char["); sb.append(i); sb.append("]='"); sb.append(myBuffer[i]); sb.append("'\t"); //byte value sb.append("byte="); sb.append(b); sb.append(" \\u"); sb.append(hexB); sb.append('\t'); //short value sb.append("short="); sb.append(s); sb.append(" \\u"); sb.append(hexS); sb.append('\t'); //Unicode Block sb.append(Character.UnicodeBlock.of(myBuffer[i])); System.out.println(sb.toString()); } System.out.println(); } /** * write content to output file * @param fileName output file name * @param content file content to write */ private static void writeFile(String fileName, String content) { try { File tmpFile = new File(fileName); if (tmpFile.exists()) { tmpFile.delete(); } FileWriter fw = new FileWriter(fileName, true); fw.write(content); fw.close(); } catch (Exception e) { System.out.println(e.toString()); } } /** * read content from input file * @param fileName input file name * @return String file content */ private static String readFile(String fileName) { try { BufferedReader fr = new BufferedReader(new FileReader(fileName)); StringBuffer out = new StringBuffer(); String thisLine = new String(); while (thisLine != null) { thisLine = fr.readLine(); if (thisLine != null) { out.append(thisLine); } } fr.close(); return out.toString(); } catch (Exception e) { System.out.print(e.toString()); return null; } } }
评论
    相关推荐
    • Unicode转码
      NULL 博文链接:https://yaoyanzhu.iteye.com/blog/1083368
    • unicode-strings
      unicode字符串 关于 编写unicode-strings模块可简化将字符串从Unicode转换为ASCII的过程。 当尝试在与扩展Unicode字符集不兼容的系统上使用字符串时,此转换可能会很有用。 1.安装 npm install git+...
    • Unicode OCR
      VS2005 2008 C# detact OCR Graph
    • Unicode Component
      支持Unicode输入显示的Edit、Memo、RichEdit、Treeview、Listview、Combogrid、Listbox,支持多种语言混合输入。可用于Delphi5、6,C++Buiilder4。提供的链接为Delphi5试用版。 操作系统:Windows2000/XP
    • Unicode2ANSI
      UNICODE2ANSI.exe是UNICODE码和ANSI码两种编码转换工具。说编码转化可能很多朋友还不是很明白,但是如果说起插入数据库的一句话“("n")%>”的加密代码“┼攠數畣整爠煥敵瑳∨≮┩>”,相信很多朋友都有所耳闻。它...
    • The UniCode
      学习visual c++编程必看,不看会后悔i,我看了 学到了好多知识
    • unicode-keyboard
      有时,表情符号会派上用场,但我永远记不起相关的 Unicode 键码。 我通过为我最喜欢的 unicode 字符创建一个带有按钮的迷你键盘来解决这个问题。 输入 Unicode 字符的键盘顺序因操作系统而异,当前实现仅适用于 ...
    • Unicode VIs.zip
      ANSI TO UTF8 , UTF8 TO ANSI
    • UniCode.rar
      Delphi演示UniCode编码、简体繁体互转的方法实现
    • Unicode.rar
      win32汇编使用定义unicode变量及数据