文件转码、序列化

1、

// 获取生成的源文件 
StringBuilder dirStr = new StringBuilder();
dirStr.append(src).append(File.separator);
String[] pkgs = pkg.split("\\.");
for (String p : pkgs) {
	dirStr.append(p).append(File.separator);
}
File dir = new File(dirStr.toString());

@SuppressWarnings("unchecked")
Collection<File> javaFiles = FileUtils.listFiles(dir, new String[] { "java" }, true);
// 如果系统文件编码不是UTF-8将对其进行转码
String sysEncoding = System.getProperty("file.encoding");
if (!"UTF-8".equalsIgnoreCase(sysEncoding) && !"UTF8".equalsIgnoreCase(sysEncoding)) {
	logger.debug(sysEncoding + " -> UTF-8 begin...");
	
	//源文件转码
	detail.append("\u25c6\u6e90\u6587\u4ef6\u8f6c\u7801(").append(sysEncoding).append("->UTF-8)").append("\n");
	for (File javaFile : javaFiles) {
		detail.append("    ").append(javaFile.getAbsolutePath());
		try {
			String javaContent = FileUtils.readFileToString(javaFile, sysEncoding);
			FileUtils.writeStringToFile(javaFile, javaContent, "UTF-8");
			// 【成功】
			detail.append(" \u3010\u6210\u529f\u3011").append("\n");
		} catch (IOException e) {
			e.printStackTrace();
			
			// 【失败】
			detail.append(" \u3010\u5931\u8d25\u3011").append("\n");
			detail.append(ExceptionUtils.getStackTrace(e));
			log.setStatus(Constants.FAILURE);
			log.setDetail(detail.toString());
			return log;
		}
	}
	logger.debug(sysEncoding + " -> UTF-8 end...");
}

 

序列化:

// 实现序列化接口
detail.append("\u25c6\u5b9e\u73b0\u5e8f\u5217\u5316\u63a5\u53e3").append("\n");
logger.debug("implSerializableInterface start...");
for (File javaFile : javaFiles) {
	try {
		String javaContent = FileUtils.readFileToString(javaFile, sysEncoding);
		if(javaContent.indexOf("@XmlType") > 0 && javaContent.indexOf("import javax.xml.bind.annotation.XmlType;") > 0){
			detail.append("    ").append(javaFile.getAbsolutePath());
			
			logger.debug(javaFile.getAbsolutePath() + " implSerializableInterface...");
			
			javaContent = implSerializableInterface(javaContent);
			// 【成功】
			detail.append(" \u3010\u6210\u529f\u3011").append("\n");
			
			FileUtils.writeStringToFile(javaFile, javaContent, "UTF-8");
		}
	} catch (IOException e) {
		e.printStackTrace();
		
		// 【失败】
		detail.append(" \u3010\u5931\u8d25\u3011").append("\n");
		detail.append(ExceptionUtils.getStackTrace(e));
		log.setStatus(Constants.FAILURE);
		log.setDetail(detail.toString());
		return log;
	}
}

 

@XmlType(name = "ESB_YS_YS_InquiryEventsReferralsInfoSrvRequest", propOrder = {
    "msgHeader",
    "timefrom",
    "timeto",
    "usercode",
    "attribute1"
})
public class ESBYSYSInquiryEventsReferralsInfoSrvRequest  {}

 

 

序列化:

@XmlType(name = "ESB_YS_YS_InquiryEventsReferralsInfoSrvRequest", propOrder = {
    "msgHeader",
    "timefrom",
    "timeto",
    "usercode",
    "attribute1",
})
public class ESBYSYSInquiryEventsReferralsInfoSrvRequest  implements java.io.Serializable{}

 

/**
 * implements java.io.Serializable
 * 
 * @param javaContent
 * @return
 */
private String implSerializableInterface(String javaContent) {
	StringBuilder sb = new StringBuilder();
	sb.append(javaContent);
	int classIdx = sb.indexOf("public class ");
	if(classIdx > 0){
		sb.insert(sb.indexOf("{", classIdx), " implements java.io.Serializable");
	}
	return sb.toString();
}

 

 int java.lang.StringBuilder.indexOf(String str, int fromIndex)
	//Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
	//从fromIndex开始查找str的索引位置
	

 StringBuilder java.lang.StringBuilder.insert(int offset, String str)
	//Inserts the string into this character sequence. 
	//The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are inserted into this sequence. 
	//在offset前插入字符串str

 。。

 

你可能感兴趣的:(序列化)