java解析moto系统自带sd卡备份短信文件vmg,motovmg,昨天刷moto mb52
分享于 点击 12349 次 点评:121
java解析moto系统自带sd卡备份短信文件vmg,motovmg,昨天刷moto mb52
昨天刷moto mb525,留下了一个让我纠结的后遗症(moto系统自动的sd卡备份工具,备份出来的短信文件vmg,在刷机后的系统居然无法导入),然后在网上拼命的找,结果许多论坛的童鞋都遇到这个问题,而且问题都没解决到。在论坛都有一个还原的sd卡apk,他妹的一装居然提示只适合三星手机使用,搞得我很纠结。
想来想去还是自己写一个东东把vmg给解析掉,再用91手机助手以xls格式把短信导入到手机(ps:生成的xls文件只适合91手机助手)。把代码和工具共享,希望能帮助到童靴们。:)
TransformStart.java
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import cn.vmg.action.TransformAction;import cn.vmg.action.TransformVMGAction;import cn.vmg.config.Config;/** * 启动UI * @author wzy * */public class TransformStart extends JFrame{ private static final long serialVersionUID = 1L; private JButton btn = null,btn2 = null; private JPanel jpanel = null, jpanel2 = null, jpanel3 = null; private JTextArea text; private JTable table = null; private JScrollPane pane = null; public TransformStart() { super(Config.TITLE); setSize(600, 500); setResizable(false);//设置最大化按钮 //DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE setDefaultCloseOperation(EXIT_ON_CLOSE); jpanel = new JPanel(); jpanel.setLayout(new FlowLayout(FlowLayout.LEFT)); jpanel2 = new JPanel(); jpanel2.setLayout(new FlowLayout(FlowLayout.RIGHT)); jpanel3 = new JPanel(); jpanel3.setLayout(new FlowLayout(FlowLayout.CENTER)); jpanel3.setSize(WIDTH, HEIGHT); btn = new JButton(Config.OPEN_FILE); btn2 = new JButton(Config.TRANSFORM_FILE); btn2.setEnabled(false); text = new JTextArea(); text.setBounds(1, 2, 2, 2); table = new JTable(); table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); pane = new JScrollPane(table); pane.setPreferredSize(new Dimension((int)getSize().getWidth()-10, (int)getSize().getHeight()-80)); } private void initStart() { JFileChooser chooser = new JFileChooser(); btn.addActionListener(new TransformAction(btn2,text,this,chooser,table)); btn2.addActionListener(new TransformVMGAction(btn2,table)); jpanel.add(text); jpanel2.add(btn); jpanel2.add(btn2); jpanel3.add(pane); add(jpanel,BorderLayout.WEST); add(jpanel2,BorderLayout.EAST); add(jpanel3,BorderLayout.SOUTH); setVisible(true); setLocation(getWidth()/2,getHeight()/3); } public static void main(String[] args) { try { new TransformStart().initStart(); } catch (Exception e) { e.printStackTrace(); } }}
TransformAction.java
package cn.vmg.action;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.table.DefaultTableModel;import cn.vmg.config.Config;import cn.vmg.filter.VMGFilter;/** * 解析VMG数据 * @author wzy * */public class TransformAction implements ActionListener { private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private Calendar calendar = Calendar.getInstance(); private JButton btn2 = null; private JFileChooser fileDialog = null; private JTextArea text = null; private JFrame jframe = null; private JTable table = null; public TransformAction(JButton btn,JTextArea text,JFrame jframe,JFileChooser chooser,JTable table) { this.btn2 = btn; this.text = text; this.jframe = jframe; this.fileDialog = chooser; this.table = table; } public void actionPerformed(ActionEvent e) { fileDialog.setFileFilter(new VMGFilter());//过虑VMG文件 int resultVal = fileDialog.showOpenDialog(jframe); //点击确定 if(resultVal == JFileChooser.APPROVE_OPTION) { String filePath = fileDialog.getSelectedFile().getAbsolutePath(); text.setText(filePath); btn2.setEnabled(true); operateFile(fileDialog, table); } } /** * 解析VMG数据 * @param chooser * @param table */ private void operateFile(JFileChooser chooser,JTable table) { if(chooser != null && chooser.getSelectedFile() != null) { File file = chooser.getSelectedFile(); Vector<String> vectorTel = new Vector<String>(), vectorDate = new Vector<String>(), vectorMsgType = new Vector<String>(), vectorText = new Vector<String>(); try { BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(file), Config.CHARSET_UTF8)); String text,temp; while((text = input.readLine()) != null) { if(text.indexOf(Config.MESSAGE_TYPE) != -1) { temp = text.substring(Config.MESSAGE_TYPE.length(),text.length()); if(temp.trim().equals(Config.MESSAGE_TYPE_DELIVER)) { vectorMsgType.add(Config.MESSAGE_TYPE_DELIVER_2); } else if(temp.trim().equals(Config.MESSAGE_TYPE_SUBMIT)) { vectorMsgType.add(Config.MESSAGE_TYPE_SUBMIT_2); } } if(text.indexOf(Config.MESSAGE_TEL) != -1) { temp = text.substring(Config.MESSAGE_TEL.length(),text.length()); vectorTel.add(temp.trim()); } if(text.indexOf(Config.MESSAGE_DATE) != -1) { temp = text.substring(Config.MESSAGE_DATE.length(),text.length()); vectorDate.add(simpleDate(temp.trim())); //读短信内容 text = input.readLine(); if(text != null) { vectorText.add(text); } } } System.err.println("vectorTel = "+vectorTel.size()+" vectorDate = "+vectorDate.size()+" vectorText = "+vectorText.size()); //组装数据 String[][] tableModel = new String[vectorTel.size()][4]; for(int i=0;i<vectorTel.size();i++) { String[] tempModel = {vectorMsgType.get(i),vectorTel.get(i),vectorDate.get(i),vectorText.get(i)}; tableModel[i]=tempModel; } //开始组装table DefaultTableModel defaultTableModels = new DefaultTableModel(tableModel, Config.HEADERS); table.setModel(defaultTableModels); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } private String simpleDate(String date) { if(date.length() < 13) { int lost = 13 - date.length(); for(int i=0;i<lost;i++) { date+="0"; } } calendar.setTimeInMillis(Long.valueOf(date)); return format.format(calendar.getTime()); }}
TransformVMGAction.java
package cn.vmg.action;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import javax.swing.JTable;import javax.swing.table.TableModel;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;import cn.vmg.config.Config;/** * 转换VMG * @author wzy * */public class TransformVMGAction implements ActionListener { private JButton btn2 = null; private JFileChooser fileDialog = null; private JTable table = null; public TransformVMGAction(JButton btn,JTable table) { this.btn2 = btn; this.table = table; } public void actionPerformed(ActionEvent e) { fileDialog = new JFileChooser(); fileDialog.setCurrentDirectory(new File("c:/"));//设置默认打开路径 fileDialog.setDialogType(JFileChooser.SAVE_DIALOG);//设置保存对话框 int resultVal=fileDialog.showDialog(null, Config.SAVE_FILE); //点击确定 if(resultVal==JFileChooser.APPROVE_OPTION) { String filePath = fileName(fileDialog.getSelectedFile().getAbsolutePath());//保存路径 operateFile(filePath); btn2.setEnabled(false); JOptionPane.showMessageDialog(null, Config.NOTICE+filePath, Config.TITLE, JOptionPane.NO_OPTION); } } /** * 生成XLS文件 * @param savePath */ private void operateFile(String savePath) { TableModel model = table.getModel(); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("SMSList"); // 创建表格头 HSSFRow rowTitle,dataRow; HSSFCellStyle headStyle = wb.createCellStyle(); headStyle.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); //添加前景色,内容看的清楚 headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headStyle.setIndention((short) 1);//表头在cell中位置 HSSFFont font = wb.createFont(); font.setColor(HSSFColor.BLUE.index); headStyle.setFont(font); //设置91手机助手短信导入Title dataRow = sheet.createRow(0); dataRow.createCell(0).setCellValue(new HSSFRichTextString(Config.MESSAGE_SMSLIST)); //插入表头 rowTitle = sheet.createRow(1); HSSFCell cell; for (int i = 0; i < model.getColumnCount(); i++) { cell = rowTitle.createCell(i); cell.setCellStyle(headStyle); cell.setCellValue(new HSSFRichTextString((model.getColumnName(i)))); } // 插入表格数据 Object obj; for (int i = 0; i < model.getRowCount(); i++) { dataRow = sheet.createRow(i + 2);//前面加了一行短信列表说明所以要加2行开始插入数据 for (int j = 0; j < model.getColumnCount(); j++) { obj = model.getValueAt(i, j); if(obj != null) { dataRow.createCell(j).setCellValue(new HSSFRichTextString(obj.toString())); } else { dataRow.createCell(j).setCellValue(new HSSFRichTextString("")); } } } // 根据内容长度自动调整excel列宽(第三列的信息内容太长报错,这里没自动调整列宽 宽度最大为255) for (int i = 0; i < model.getColumnCount()-2; i++) { sheet.autoSizeColumn(i); } FileOutputStream fileOut = null; try { fileOut = new FileOutputStream(savePath); wb.write(fileOut); fileOut.close(); } catch (IOException e) { try { fileDialog.getSelectedFile().deleteOnExit(); wb.write(fileOut); fileOut.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } } private String fileName(String name) { StringBuffer buff = new StringBuffer(); buff.append(name); buff.append(Config.SUFFIX_XLS); return buff.toString(); }}
Config.java
package cn.vmg.config;public class Config { public static final String TITLE = "VMG转换Excel"; public static final String OPEN_FILE = "打开文件"; public static final String TRANSFORM_FILE = "转换Excel"; public static final String CHANGE_FILE = "选择需要打开文件"; public static final String ONLY_FILE = "*.vmg"; public static final String CANOPEN_FILE = ".vmg"; public static final String SAVE_FILE = "保存文件"; public static final String[] HEADERS = {"SMSType_类型", "Name_姓名", "Time_时间", "Content_短信内容" , "Flag_标志"}; public static final String CHARSET_UTF8 = "UTF-8"; public static final String CHARSET_GBK = "GBK"; public static final String CHARSET_ISO = "ISO-8859-1"; public static final String SUFFIX_XLS = ".xls"; public static final String SUFFIX_XLSX = ".xlsx"; public static final String SUFFIX_CSV = ".csv"; public static final String NOTICE = "转换成功!"+SUFFIX_XLS+"存放在:"; public static final String MESSAGE_TYPE = "X-MESSAGE-TYPE:"; public static final String MESSAGE_TEL = "TEL:"; public static final String MESSAGE_DATE = "Date"; public static final String MESSAGE_TYPE_DELIVER = "DELIVER";//接收 public static final String MESSAGE_TYPE_DELIVER_2 = "Receive_收件";//接收 public static final String MESSAGE_TYPE_SUBMIT = "SUBMIT";//发送 public static final String MESSAGE_TYPE_SUBMIT_2 = "Send_发件";//发送 public static final String MESSAGE_SMSLIST = "SMSList_短信列表";}
VMGFilter.java
package cn.vmg.filter;import java.io.File;import javax.swing.filechooser.FileFilter;import cn.vmg.config.Config;/** * VMG过虑 * @author wzy * */public class VMGFilter extends FileFilter { /** * 重写接收文件方法 * @return * true 表示显示出来 </br> * false 表示不显示出来 */ public boolean accept(File f) { if(f.isDirectory()) { return true; } return f.getName().endsWith(Config.CANOPEN_FILE); } /** * 这就是显示在打开框中 */ public String getDescription() { return Config.ONLY_FILE; }}
用户点评