JDK自带的工具native2ascii可以将uncode编码的文件转换为本地编码的文件,但是不能批量转换文件。现在介绍小工具可以批量转换所有的文件。
首先下载工具UncodeReverse.exe,下载地址:http://download.csdn.net/source/344466,如果想自己编译程序,可以继续向下看。
使用命令编译打包过程如下:
UncodeReverse D:
src
将d:/src下所有子目录中的资源文件(*.properties文件),按照Unicode转换为本地编码,转换后的文件名后加“.reverse”。
UncodeReverse D:
src
"
*.java
"
将d:/src下的所有java文件的uncode编码转换为本地编码,并且文件名称后加“.reverse”。
UncodeReverse D:
src
"
*_zh.properties
"
将d:/src下所有子目录中的所有中文资源文件文件,按照Unicode转换为本地编码,转换后的文件名后加“.reverse”。
命令的输出如下:
d:
src
allMessages_zh_CN
.
properties-
>
allMessages_zh_CN
.
properties
.
reverse success
.
程序代码如下:
/**/
/*
* UncodeReverse.java
* ZhaoHonghui
*/
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileFilter;
import
java.io.FileReader;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.util.regex.Pattern;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* 将利用 native2ascii 命令编码的资源文件反向. <br>
* native2ascii < resource.properties > resource_zh_CN.properties<br>
* java -jar UncodeReverse.jar<br>
*
* @author ZhaoHonghui
* @version 1.0d
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
UncodeReverse
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final String VERSION = "v1.0f";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static boolean SkipComment = false;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String args[]) ...{
String path = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 默认匹配所有的资源文件.
*/
String fileName = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < args.length; i++) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (args[i].startsWith("-S")) ...{
SkipComment = true;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (path == null) ...{
path = args[i];
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else if (fileName == null) ...{
fileName = args[i];
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (path == null) ...{
usage();
System.exit(-1);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (fileName == null) ...{
fileName = "*.properties";
}
File file = new File(path);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (!file.isDirectory()) ...{
System.out.println("[" + file + "] not exists or not directory.");
usage();
System.exit(-1);
}
reverseProperties(file, new UncodeFileFilter(fileName));
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static File[] reverseProperties(File file, FileFilter filter) ...{
File[] files = file.listFiles(filter);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < files.length; i++) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (files[i].isDirectory()) ...{
reverseProperties(files[i], filter);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
File source = files[i];
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 文件名后添加.reverse后缀.
*/
File target = new File(files[i].getParent(), files[i].getName()
+ ".reverse");
// if (target.exists()) {
// System.out.println("Target file [" + target
// + "] exits,break.");
// } else {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (decodeFile(source, target)) ...{
System.out.println(source + "->" + target.getName()
+ " success.");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
System.out.println(source + " error.");
target.delete();
}
// }
}
}
return files;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static boolean decodeFile(File source, File target) ...{
BufferedReader read = null;
BufferedWriter writer = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
read = new BufferedReader(new FileReader(source));
writer = new BufferedWriter(new FileWriter(target));
writer.write("# Reverse by UncodeReverse " + VERSION
+ ". Copyright 2007 ZhaoHonghui (zhaohonghui@hotmail.com) "
+ "http://blog.csdn.net/z3h .");
writer.newLine();
writer.write("# Source File Name:" + source.getAbsolutePath());
writer.newLine();
String s = read.readLine();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while (s != null) ...{
String t = decodeString(s);
writer.write(t);
writer.newLine();
s = read.readLine();
}
return true;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (IOException e) ...{
System.out.println(e.getClass() + ":" + e.getMessage());
return false;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} finally ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (read != null) ...{
read.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (writer != null) ...{
writer.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (IOException e) ...{
e.printStackTrace();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static String decodeString(String str) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (SkipComment && str.trim().startsWith("#")) ...{
return str;
}
StringBuffer buf = new StringBuffer(str.length());
boolean flag = false;
boolean uflag = false;
int cnt = 0;
char[] uncode = new char[4];
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < str.length(); i++) ...{
char c = str.charAt(i);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (c == '/') ...{
flag = true;
continue;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (flag && c == 'u') ...{
uflag = true;
continue;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (flag && !uflag) ...{
buf.append("/");
}
flag = false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (uflag && cnt < 5) ...{
uncode[cnt] = c;
cnt++;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (uflag && cnt == 4) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
buf.append((char) Integer.parseInt(new String(uncode),
16));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (NumberFormatException Nfe) ...{
buf.append("/u" + new String(uncode));
}
flag = false;
uflag = false;
cnt = 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
buf.append(c);
}
}
return buf.toString();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static void usage() ...{
System.out
.println("UncodeReverse "
+ VERSION
+ ". Copyright 2007 ZhaoHonghui (zhaohonghui@hotmail.com) http://blog.csdn.net/z3h.");
System.out
.println("Usage: UncodeReverse [option(s)] <path> [fileName(s)]");
System.out.println(" Options -S:Skip Comment.");
System.out
.println(" FileName(s):"*.properties","*_zh.properties","resource*_zh_CN.properties"");
System.out
.println("Sample: UncodeReverse D:/src "*_zh.properties"");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
static class UncodeFileFilter implements FileFilter ...{
String fileNameExtra;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
UncodeFileFilter(String fileNameExtra) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//*
* 将. 替换为 . 将* 替换为.*
*/
fileNameExtra = fileNameExtra.replaceAll("/.", "//.");
fileNameExtra = fileNameExtra.replaceAll("/*", "/./*");
this.fileNameExtra = fileNameExtra.toLowerCase();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public boolean accept(File pathname) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (pathname.isDirectory()) ...{
return true;
}
if (Pattern.matches(this.fileNameExtra, pathname.getName()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
.toLowerCase())) ...{
return true;
}
return false;
}
}
}
1、将java编译为Class文件。
2、将Class打包为jar文件。
3、使用使用exe4j将jar文件转换为exe文件。
可以从这里下载exe4j:http://download.csdn.net/source/296007。