1. 程式人生 > >將三星手機備忘錄vnt格式檔案轉為txt格式備份

將三星手機備忘錄vnt格式檔案轉為txt格式備份

最近想把手機上的備忘錄匯出到電腦上備份,結果發現匯出來的並不是txt格式,而是三星自己的vnt格式。於是自己用java寫了幾行程式碼把它轉為txt格式用於備份。留著以後備份的時候用。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetSamSungVntInfo {

    public static void main(String[] args) throws IOException {
	//記事本vnt檔案的放置資料夾
        File dir = new File("samsungvnt");
        File[] files = dir.listFiles();
	//結果輸出目錄
        String out = "result/";
        Map<String, String> map = new HashMap<String, String>();
        for (File file : files) {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String tmp = null;
            String body = null;
            String time = null;
            while ((tmp = reader.readLine()) != null) {
                if (tmp.startsWith("BODY")) {
                    body = tmp.split(":")[1];
                } else if (tmp.startsWith("LAST-MODIFIED")) {
                    time = tmp.split(":")[1];
                }
            }
            reader.close();
            if (time != null && body != null)
                map.put(time, body);
        }

        Map<String, String> result = new HashMap<String, String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String body = entry.getValue().trim();
            String time = entry.getKey();
            String[] tmps = null;
            List<Byte> bytes = new ArrayList<Byte>();
            if (!body.startsWith("=")) {
                tmps = body.split("=", 2);
                String tmp = tmps[0];
                for (byte b : tmp.getBytes()) {
                    bytes.add(b);
                }
                if (tmps.length > 1) {
                    tmps = tmps[1].split("=");
                } else {
                    tmps = new String[] {};
                }
            } else {
                tmps = body.split("=", 2)[1].split("=");
            }
            for (String tmp : tmps) {
                String tmp1 = tmp.substring(0, 2);
                bytes.add((byte) Integer.parseInt(tmp1, 16));
                String tmp2 = tmp.substring(2);
                if (tmp2.length() > 0) {
                    for (byte b : tmp2.getBytes()) {
                        bytes.add(b);
                    }
                }
            }
            int size = bytes.size();
            byte[] bs = new byte[size];
            for (int i = 0; i < size; i++) {
                bs[i] = bytes.get(i);
            }
            String info = new String(bs, "utf8");
            result.put(time, info);
        }
        File outTxtFile = new File(out + System.currentTimeMillis() + ".txt.back");
        PrintWriter writer = new PrintWriter(outTxtFile);

        writer.println(System.currentTimeMillis() + "==========backup");
        List<String> times = new ArrayList<String>(result.keySet());
        Collections.sort(times);
        Collections.reverse(times);
        writer.println("=============================");
        for (String time : times) {
            writer.println(time + ":");
            writer.println(result.get(time));
            writer.println("=============================");
        }
        writer.close();
        System.out.println("處理完畢");
    }
}