`
butnet
  • 浏览: 84984 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Struts资源文件处理工具

阅读更多

一、创建置换工具

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

/**
* 字符转换工具
*/
public class Native2Ascii {
    private static String fromFile = null;
    private static String toFile = null;
    private static String toSrcFile = null;

    /**
     * 将字符串str中的非Ascii字符转换为\uFFFF格式
     *
     * @param str
     *            需要转换的字符
     * @return 转换结果
     */
    public static String toAscii(String str) {
        if (str.trim().startsWith("#"))
            return str;
        StringBuffer re = new StringBuffer();
        char[] chs = str.toCharArray();
        for (char c : chs) {
            if (c > 255)
                re.append("\\u" + Integer.toHexString(c));
            else
                re.append(c);
        }
        return re.toString();
    }

    /**
     * Native2Ascii 原文件 新文件路径
     *
     * @throws URISyntaxException
     */
    public static void main(String args[]) throws IOException,
            URISyntaxException {
        if (args == null || args.length == 0) {
            File file = new File(Native2Ascii.class.getResource("/N2A.ini")
                    .toURI());
            if (!file.exists()) {
                file.createNewFile();
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("#从资源文件");
                writer.newLine();
                writer.write("From File = ");
                writer.newLine();
                writer.write("#到资源文件");
                writer.newLine();
                writer.write("To File = ");
                writer.newLine();
                writer.write("#到本地源文件");
                writer.newLine();
                writer.write("Src File = ");
                writer.newLine();
                writer.close();
                System.out.println("配置文件不存在,以默认方式创建 \"N2A.ini\" 文件");
                return;
            } else {
                InputStream in = Native2Ascii.class
                        .getResourceAsStream("/N2A.ini");
                if (!readConfig(in))
                    return;
            }
        } else if (args == null || args.length != 3) {
            System.out.println("Native2Ascii 原文件 新文件路径");
            return;
        } else {
            fromFile = args[0];
            toFile = args[1];
            toSrcFile = args[2];
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(Native2Ascii.class.getResourceAsStream(fromFile)));
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(Native2Ascii.class.getResource(toFile).toURI())));
        BufferedWriter srcWriter = new BufferedWriter(new FileWriter(toSrcFile));
        String temp;
        while ((temp = reader.readLine()) != null) {
            temp=toAscii(temp);
            writer.write(temp);
            writer.newLine();
            srcWriter.write(temp);
            srcWriter.newLine();
        }
        reader.close();
        writer.close();
        srcWriter.close();
    }

    private static boolean readConfig(InputStream in) throws IOException {
        BufferedReader reader=new BufferedReader(new InputStreamReader(in));
        String temp,name,key;
        int index,line=0;
        while((temp=reader.readLine())!=null){
            line++;
            if(temp.trim().length()==0)continue;
            if(temp.trim().startsWith("#"))continue;
            if(temp.trim().startsWith("!"))continue;
            index=temp.indexOf("=");
            if(index==-1){
                System.out.println("Error: Line "+line+" is not format \"name=value\"");
                continue;
            }
            name=temp.substring(0, index).trim();
            key=temp.substring(index+1).trim();
            if(name.equalsIgnoreCase("From File")){
                fromFile=key;
            }else if(name.equalsIgnoreCase("To File")){
                toFile=key;
            }else if(name.equalsIgnoreCase("Src File")){
                toSrcFile=key;
            }
        }
        boolean ok=true;
        if(fromFile==null||fromFile.length()==0){
            ok=false;
            System.out.println("Error: \"From File\" value is NULL");
        }
        if(toFile==null||toFile.length()==0){
            ok=false;
            System.out.println("Error: \"To File\" value is NULL");
        }
        if(toSrcFile==null||toSrcFile.length()==0){
            ok=false;
            System.out.println("Error: \"Src File\" value is NULL");
        }
        return ok;
    }
}   

二、注意此工具的配置文件N2A.ini

#资源文件 的源文件
From File = /MyResources.txt
#Struts配置的资源文件
To File = /MessageResources.properties
#Struts配置的资源文件的源文件
Src File = src/MessageResources.properties

三、效果
/MyResources.txt 文件内容如下:

#全局信息
system.name = 影城管理系统
system.version = Vesion 1.0
system.copy = 2008-2020
page.title = 欢迎进入影城管理系统
type.null = 请求类型不能为空

运行置换工具得到文件:/MessageResources.properties  src/MessageResources.properties 文件内容如下:

#全局信息
system.name = \u5f71\u57ce\u7ba1\u7406\u7cfb\u7edf
system.version = Vesion 1.0
system.copy = 2008-2020
page.title = \u6b22\u8fce\u8fdb\u5165\u5f71\u57ce\u7ba1\u7406\u7cfb\u7edf
type.null = \u8bf7\u6c42\u7c7b\u578b\u4e0d\u80fd\u4e3a\u7a7a

分享到:
评论

相关推荐

    struts中文资源文件转换工具

    struts中文资源文件转换工具,struts中资源文件中文转16进制码小工具

    Struts的文件下载

    博文链接:https://ychw668.iteye.com/blog/122843

    Struts2实现文件的上传和下载

    开发工具myeclipse 用到的jar包有:asm-3.3.jar,asm-commons-3.3.jar,asm-tree-3.3.jar,commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.5.jar,freemarker-2.3.18.jar,javassist-3.11.0....

    Struts2 in action中文版

    4.3.1 工具拦截器 67 4.3.2 数据转移拦截器 67 4.3.3 工作流拦截器 69 4.3.4 其他拦截器 72 4.3.5 内建的拦截器栈 73 4.4 声明拦截器 74 4.4.1 声明独立的拦截器和拦截器栈 74 4.4.2 将拦截器映射到动作组件 76 ...

    struts2part2

    struts2part2,由于文件太大无法一次上传,分成两部分,请用文件合并工具进行合并。文件合并工具见本人资源。

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    struts2.0官方项目之四(showcase)

    struts2.0官方项目之四(showcase) <br>=================================================== Struts2.0官方项目共4个,名字如下: <br>blank mailreader portlet showcase <br>这4个官方...

    NetBeans下的Struts2.0插件

    让NetBeans支持Struts2,是两个.nbm文件,解压缩后在NetBeans中点工具--->插件--->点已下载--->添加插件--->选择解压的文件即可

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    struts2.0官方项目之一(blank)

    struts2.0官方项目之一(blank) <br>=================================================== Struts2.0官方项目共4个,名字如下: <br>blank mailreader portlet showcase <br>这4个官方项目...

    struts2.0官方项目之三(portlet)

    struts2.0官方项目之三(portlet) <br>=================================================== Struts2.0官方项目共4个,名字如下: <br>blank mailreader portlet showcase <br>这4个官方项目都...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (2)

    12.6.15 编写Struts的配置文件struts-config.xml 12.6.16 编写Spring和Hibernate的配置文件spring-config.xml 12.6.17 编写web.xml 12.6.18 验证示例 12.7 小结 第四篇 J2EE项目案例精选 第十三章 网上调查系统 13.1...

    源码基于JSP的博客系统(struts+hibernate+spring).rar

    它包含用于构建一个功能齐全的博客系统的源代码和资源文件,结合了Java Server Pages (JSP)、Apache Struts框架、Hibernate持久化框架以及Spring框架的强大功能,以支持高效的数据库操作和灵活的业务逻辑处理。...

    SSH(Struts+spring+hibernate)整合资料

    struts,hibernate的整合)资料(1) 中大软件工厂项目前培训资料(1) 完整笔记+源码(1) ssh(1) C#查询参数化例子(1) 学习笔记+完整源码(1) 介绍与深入(1) 学习笔记(不含整合)(1) C#(1) .CHM格式文件制作工具(很好用的一...

    java初学者的工具ppt文件

    如果想学Java EE(对不起,我不了解JavaME,所以我无法涉及JavaME的相关内容),对于AWT、Swing是否要学习呢,我个人是觉得还是要知道其所以然的,特别是其事件处理模式,我强烈建议初学者一定要弄清楚,其他具体的...

    Java资源包01

    Tomcat Native 这个项目可以让 Tomcat 使用 Apache 的 apr 包来处理包括文件和网络IO操作,以提升性能。 预输入搜索 Cleo Cleo 是一个灵活的软件库用于处理一些预输入和自动完成的搜索功能,该项目是 LinkedIn 公司...

    基于Spring+Struts+Mybatis的B2C电子商务网站系统源码+项目说明.zip

    【资源说明】 基于Spring+Struts+Mybatis的B2C电子商务网站系统源码+项目说明.zip 可以通过在后台登录之后的界面添加相应的商品类别来扩展为任意的B2C网站。 Resource 所有的代码文件都在src目录下,主要包括的就是...

    验证码工具类.zip

    web开发验证码工具类 servlet版和struts2(Action)版两个文件夹,资源包含验证码源码.java文件,.jar包,和使用说明,以及代码样例

    Struts in Action中文版

    2.6.2. Struts的强项........................................................................................................58 Struts in Action 中文版 Lastest Revised:10/14/2005 10:27:00 AM ...

    PropertyEditor

    又一个中文转unicode的工具,在创建struts资源文件的时候,非常有用,输入中文后,直接保存就自动转换了

Global site tag (gtag.js) - Google Analytics