浏览 2098 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-04
* 字符串转化为unicode * @param gbString * @return */ public static String encodeUnicode(final String gbString) { char[] utfBytes = gbString.toCharArray(); String unicodeBytes = ""; for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) { String hexB = Integer.toHexString(utfBytes[byteIndex]); if (hexB.length() <= 2) { hexB = "00" + hexB; } //unicodeBytes = unicodeBytes + "\\u" + hexB; unicodeBytes = unicodeBytes + hexB; } System.out.println(unicodeBytes); return unicodeBytes; } // unicode转化汉字 public static StringBuffer decodeUnicode(String utfStr) { final StringBuffer buffer = new StringBuffer(); String charStr = ""; String operStr = utfStr; for(int i =0 ; i < utfStr.length() ;i=+4){ charStr = operStr.substring(0, 4); operStr = operStr.substring(4, operStr.length()); char letter = (char) Integer.parseInt(charStr, 16); buffer.append(new Character(letter).toString()); } return buffer; } /** * 信息按长度截取 * @param messages * @param length 截取长度 * @param vmsg * @return */ public static Vector<String> splitString(String messages, int length, Vector<String> vmsg) { if (messages.length() <= length) { vmsg.add(messages); } else { vmsg.add(messages.substring(0, length)); messages = messages.substring(length); vmsg = splitString(messages,length,vmsg); } return vmsg; } /** * 用utf-8格式保存文件 * @param fileName * @param msg */ public static void outputFile(String fileName, String msg) { File file = getFileByFileName(fileName); Writer ow = null; try { // FileWriter fw = new FileWriter(file); // fw.write(msg); // fw.flush(); // byte[] bytes = msg.getBytes("GBK"); // String str = new String(bytes, "GBK"); // OutputStream os = new FileOutputStream(file); // os.write(bytes); // os.close(); ow = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); ow.write(msg); } catch (IOException e) { System.out.println(e); } finally { //关闭reader if (null != ow) { try { ow.close(); } catch (IOException e) { System.out.println(e); } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |