获取邀请码  基地首页 | 基地文章 | 基地动画 | 基地软件 | 技术论坛 | 会员学院
 
 
热门搜索:安全 站长培训 网站赚钱 会员培训 CMS建站网 安全
攻防总结免费资源网赚文章网管技巧防火墙技术端口入侵Sniffer嗅探arp技术
DDOS攻防3389攻防灰鸽子文章逆向工程破解实例加密技术脱壳技术溢出漏洞
serv-u漏洞社会工程学渗透技术跨站技术提权技术
 您现在的位置: 新世纪网安基地 >> 文章 >> 编程技术 >> 文章正文
Java字符集编码与转码
作者:佚名    文章来源:转载    点击数:    更新时间:2009-11-30

Java字符集编码与转码
 
Java字符的class代码总是Unicode字符集的UTF-16编码,运行时内存中的字符串在没有指定编码的时候也总是Unicode编码。
 
Java编译时候,会将java文件的编码按照指定编码或者(系统默认的)编码转换为Unicode并加载到内存中进行编译。
 
下面给出一个Java转码工具,没有测试过,呵呵:
 
package lavasoft.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;

/**
* 转码工具,全面支持文件、字符串的转码
*
* @author Administrator 2009-11-29 16:14:21
*/

public class EncodingToolkit {
        private static Log log = LogFactory.getLog(EncodingToolkit.class);

        public static void main(String[] args) {
                String han = "汉";


                System.out.println("---------");
        }

        /**
         * 对字符串重新编码
         *
         * @param text                字符串
         * @param resEncoding 源编码
         * @param newEncoding 新编码
         * @return 重新编码后的字符串
         */

        public static String reEncoding(String text, String resEncoding, String newEncoding) {
                String rs = null;
                try {
                        rs = new String(text.getBytes(resEncoding), newEncoding);
                } catch (UnsupportedEncodingException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是使用了不支持的字符编码");
                        throw new RuntimeException(e);
                }
                return rs;
        }

        /**
         * 重新编码Unicode字符串
         *
         * @param text                源字符串
         * @param newEncoding 新的编码
         * @return 指定编码的字符串
         */

        public static String reEncoding(String text, String newEncoding) {
                String rs = null;
                try {
                        rs = new String(text.getBytes(), newEncoding);
                } catch (UnsupportedEncodingException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是使用了不支持的字符编码" + newEncoding);
                        throw new RuntimeException(e);
                }
                return rs;
        }

        /**
         * 文本文件重新编码
         *
         * @param resFile         源文件
         * @param resEncoding 源文件编码
         * @param distFile        目标文件
         * @param newEncoding 目标文件编码
         * @return 转码成功时候返回ture,否则false
         */

        public static boolean reEncoding(File resFile, String resEncoding, File distFile, String newEncoding) {
                boolean flag = true;
                InputStreamReader reader = null;
                OutputStreamWriter writer = null;
                try {
                        reader = new InputStreamReader(new FileInputStream(resFile), resEncoding);
                        writer = new OutputStreamWriter(new FileOutputStream(distFile), newEncoding);
                        char buf[] = new char[1024 * 64];         //字符缓冲区
                        int len;
                        while ((len = reader.read(buf)) != -1) {
                                writer.write(buf, 0, len);
                        }
                        writer.flush();
                        writer.close();
                        reader.close();
                } catch (FileNotFoundException e) {
                        flag = false;
                        log.error("没有找到文件,转码发生异常!");
                        throw new RuntimeException(e);
                } catch (IOException e) {
                        flag = false;
                        log.error("读取文件为一个内存字符串失败,失败原因是读取文件异常!");
                        throw new RuntimeException(e);
                } finally {
                        if (reader != null) try {
                                reader.close();
                        } catch (IOException e) {
                                flag = false;
                                throw new RuntimeException(e);
                        } finally {
                                if (writer != null) try {
                                        writer.close();
                                } catch (IOException e) {
                                        flag = false;
                                        throw new RuntimeException(e);
                                }
                        }
                }
                return flag;
        }

        /**
         * 读取文件为一个Unicode编码的内存字符串,保持文件原有的换行格式
         *
         * @param resFile    源文件对象
         * @param encoding 文件字符集编码
         * @return 文件内容的Unicode字符串
         */

        public static String file2String(File resFile, String encoding) {
                StringBuffer sb = new StringBuffer();
                try {
                        LineNumberReader reader = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(resFile), encoding)));
                        String line;
                        while ((line = reader.readLine()) != null) {
                                sb.append(line).append(System.getProperty("line.separator"));
                        }
                        reader.close();
                } catch (UnsupportedEncodingException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是使用了不支持的字符编码" + encoding);
                        throw new RuntimeException(e);
                } catch (FileNotFoundException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因所给的文件" + resFile + "不存在!");
                        throw new RuntimeException(e);
                } catch (IOException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是读取文件异常!");
                        throw new RuntimeException(e);
                }
                return sb.toString();
        }

        /**
         * 使用指定编码读取输入流为一个内存Unicode字符串,保持文件原有的换行格式
         *
         * @param in             输入流
         * @param encoding 构建字符流时候使用的字符编码
         * @return Unicode字符串
         */

        public static String stream2String(InputStream in, String encoding) {
                StringBuffer sb = new StringBuffer();
                LineNumberReader reader = null;
                try {
                        reader = new LineNumberReader(new BufferedReader(new InputStreamReader(in, encoding)));
                        String line;
                        while ((line = reader.readLine()) != null) {
                                sb.append(line).append(System.getProperty("line.separator"));
                        }
                        reader.close();
                        in.close();
                } catch (UnsupportedEncodingException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是使用了不支持的字符编码" + encoding);
                        throw new RuntimeException(e);
                } catch (IOException e) {
                        log.error("读取文件为一个内存字符串失败,失败原因是读取文件异常!");
                        throw new RuntimeException(e);
                } finally {
                        if (in != null) try {
                                in.close();
                        } catch (IOException e) {
                                log.error("关闭输入流发生异常!", e);
                                throw new RuntimeException(e);
                        }
                }
                return sb.toString();
        }

        /**
         * 字符串保存为制定编码的文本文件
         *
         * @param text         字符串
         * @param distFile 目标文件
         * @param encoding 目标文件的编码
         * @return 转换成功时候返回ture,否则false
         */

        public static boolean string2TextFile(String text, File distFile, String encoding) {
                boolean flag = true;
                if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
                OutputStreamWriter writer = null;
                try {
                        writer = new OutputStreamWriter(new FileOutputStream(distFile), encoding);
                        writer.write(text);
                        writer.close();
                } catch (IOException e) {
                        flag = false;
                        log.error("将字符串写入文件发生异常!");
                        throw new RuntimeException(e);
                } finally {
                        if (writer != null) try {
                                writer.close();
                        } catch (IOException e) {
                                log.error("关闭输出流发生异常!", e);
                                throw new RuntimeException(e);
                        }
                }
                return flag;
        }
}
 
 

『关闭该页』 『打印该页』

  • 上一篇文章:
  • 责任编辑:Wangtianxiang 
  • 下一篇文章:
  • 最近更新
    推荐文章公益讲座:,全面提升网
    推荐文章站长讲座第5期-站长快
    推荐文章高效学习 成就未来富站
    推荐文章全面提高执行力,让网络
    推荐文章服务器超级隐藏账户的
    推荐文章小黑哥:月收入3000的
    推荐文章黑客横行,教你设个陷阱
    推荐文章批处理学习完全教程
    推荐文章安全技巧:检查自己的
    推荐文章多种方法:揪出隐藏在电
    热门文章
    普通文章asp记录管理员账号密码
    普通文章DZ7.1 and 7.2 0远程代
    普通文章破解qq2009聊天记录ms
    普通文章跑跑卡丁车利用金山网
    普通文章网友和黑客都最爱“1
    普通文章干掉360的两种方法
    普通文章五种有效的淘宝客推广
    普通文章百度被黑原理,图解
    固顶文章活动:终身会员打折疯狂
    普通文章黑鹰科技公司开办黑客
    相关文章
    高校网站寒假被黑 疑因黑
    怎样在网上赚钱 看我怎么
    个人网赚月薪20000的起步
    巧用电子书推广淘宝客日
    浅析网赚新手日赚50元步
    淘宝客日赚过百几点经验
    IE极光0day漏洞攻击飙升
    1个淘宝差评引发上百骚扰
    Windows Media Player 1
    Internet Explorer 远程
    2010.01.19病毒预警:“
    红衫网赚:淘宝客日赚过
    百度李一男辞职 转12580
    考试挂科 大学生黑掉学校
    内网恢复 谷歌中国回复正
    SwiFTP v1.11 DOS攻击
    SQL注入的新技巧
    我的开店两周进帐1253.0
    谈我淘客月入1000的几点
    攻击谷歌的IE代码公之于
    关于我们 | 加入会员 | 网安商城 | 投稿方法 | 广告报价| 友情连接 | 网站地图 | 网安论坛网安招聘 |站长
    加入会员咨询QQ:65444425 投稿方法与教程欺骗错误举报QQ:21611201 广告与合作QQ:9324223

    浙ICP备06031184号