文件转码、序列化
来源:互联网 发布:vue.js 绑定日期格式 编辑:程序博客网 时间:2024/05/16 23:57
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
。。
0 0
- 文件转码、序列化
- 序列化xml文件
- 序列化 文件 Serialize
- 序列化对象到文件
- Hadoop序列化文件SequenceFile
- Hadoop序列化文件SequenceFile
- XmlSerializer序列化XML文件
- xml文件的序列化
- STL文件序列化读取
- xml文件的序列化
- zzh29_xml文件的序列化
- xml文件的序列化
- Android序列化xml文件
- hadoop文件的序列化
- xml文件序列化方式
- 从文件序列化出对象(使用xml序列化)
- Java 序列化反序列化文件
- 利用序列化进行文件读写
- Android 根据屏幕尺寸适配控件尺寸(按比例缩放)
- 生成器generator
- 算法导论 桶排序
- 【例子】
- Android不太能够分辨率launcher icon的适配
- 文件转码、序列化
- Redis 与 数据库处理数据的两种模式
- Android实现可自动关闭的定时器
- 01、React系列之--React简介
- 在word中插入高亮缩进的代码
- linux中mv命令使用
- Android短信监听功能(解决onChange触发两次的问题)
- udp、tcp 杂项
- 一个正方形截取四分之一后如何四等分