native2ascii命令行不支持批量转换,用下面程序可以对文件夹进行扫描并做批量转换。
package encode;
import java.io.File;
/**
* 用native2ascii批量转换java源码
* @author wzjin
*
* 删除并重命名
* del D:\文件路径\src\thostftdcmdapi\*.java
* rename D:\文件路径\src\thostftdcmdapi\*.java-1 *.java
*
*/
public class EncodeConverter
{
/**
* 用循环的方法读取文件夹,将其中的文本文件转为字符串
*
* @param path
* 文件路径,可以是文件路径或文件夹路径
* @param fileExtName
* 需要读取的文件扩张名。 Created:2013-11-11
* @author:wzjin
*/
public static void convertDir(String path, String... fileExtName) {
Runtime runtime = Runtime.getRuntime();
File file = new File(path);
if (file.isFile()) {
String filename = file.getName().toLowerCase();
if (endwith(filename, fileExtName))
{
// 这里做转换
String str="native2ascii -reverse "+ path+" "+ path+"-1";
try {
runtime.exec(str);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
File[] files = file.listFiles();
String[] filenames = file.list();
if (filenames == null)
return;
for (int i = 0; i < filenames.length; i++) {
convertDir(files[i].getPath(), fileExtName);// 慎用不定参数,如果这里没有参数输入,ide并不会给你提示
}
}
}
/**
* 对上面生成的文件夹进行清理,删除以前文件,将新文件改为以前的文件名(没有完成);
* @param path
*/
public static void renameDir(String path)
{
Runtime runtime = Runtime.getRuntime();
File file = new File(path);
if (file.isFile()) {
} else {
File[] files = file.listFiles();
String[] filenames = file.list();
if (filenames == null)
return;
for (int i = 0; i < filenames.length; i++) {
renameDir(files[i].getPath());// 慎用不定参数,如果这里没有参数输入,ide并不会给你提示
}
}
}
/**
* 返回是否需要读取的文件
*
* @param extname
* @param strings
* @return Created:2013-11-11
* @author:wzjin
*/
private static boolean endwith(String filename, String... fileExtNames) {
String fileExtName = filename.toLowerCase();
for (int i = 0; i < fileExtNames.length; i++) {
if (fileExtName.endsWith(fileExtNames[i].toLowerCase())) {
return true;
}
}
return false;
}
public static void main(String[] args)
{
EncodeConverter.convertDir("D:/dm/out", "java");
}
}