1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vmg形式のファイルをeml形式で出力するjava

Last updated at Posted at 2017-09-02

eml形式にするためのメッセージ単位のタグの間をファイルに書き出すだけの、
単純なもの。作った経緯は以下のページ参照。
Androidスマホのキャリアメールを取り出す方法

vmgは複数のメッセージデータがタグに囲まれて入っている。タグに関しては、以下を参考にした。
PowerShell で VMG ファイルを変換してエクセルで読む


import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;

class vmg2Eml{
    public static void main(String args[]){
				String input_maildata_filename;
       if(args.length <= 0){
            System.out.println("コマンドラインにてファイル名を");
						return;
        } else {
					input_maildata_filename = args[0];
        }
        try{
						int line_number = 0;
            File file = new File(input_maildata_filename);

						if (!file.exists()){
							System.out.println("ファイルはありません");
							return;
						}

						FileInputStream FIS = new FileInputStream(file);
						BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(FIS,"UTF-8"));
						String data;
						String strBegin = "BEGIN:VBODY";
						String strEnd = "END:VBODY";
						Boolean Has2Write = false;
						int mail_number = 0;

						PrintWriter printwriter = null;
						FileOutputStream FOS2write = null;

						while ((data = bufferedReader.readLine()) != null) {
							int isBegin = data.indexOf(strBegin);
							int isEnd = data.indexOf(strEnd);
							
							if(isBegin != -1){
								Has2Write = true;
								data = bufferedReader.readLine();
								mail_number++;
								String filename2write = String.format("%05d",mail_number) + ".eml";
								File file2write = new File(filename2write);
								FOS2write = new FileOutputStream(file2write);
								printwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(FOS2write,"UTF-8")));
							}

							if(isEnd != -1){
								Has2Write = false;
								printwriter.close();
							}

							if(printwriter != null && Has2Write){
								printwriter.println(data);
							}
						}

            bufferedReader.close();

        }catch(FileNotFoundException e){
            System.out.println(e);
        }catch(IOException e){
            System.out.println(e);
        }
    }
}

//System.out.println(data);

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?