OSDN Git Service

2019-02-11
[hayashilib/hayashi.git] / src / jp / co / areaweb / tools / command / JSendmail.java
1 package jp.co.areaweb.tools.command;\r
2 import java.io.*;\r
3 \r
4 /**\r
5  * 実際にはメールへの送信を行わずに、指定された情報をファイルに吐き出す。\r
6  * 「/usr/sbin/sendmail」の代替コマンド\r
7  * @author hayashi\r
8  */\r
9 public class JSendmail\r
10 {\r
11     /** \r
12      *  サンプル\r
13      *  exp: java jp.co.areaweb.tools.command.JSendmail [parameter]\r
14      * @param args parameters\r
15      * @throws  Exception       例外\r
16      */\r
17     static public void main(String[] args) throws Exception {\r
18         String commandLine = "";\r
19         for (int i=0; i < args.length; i++) {\r
20             if (i != 0) {\r
21                 commandLine += " ";\r
22             }\r
23             commandLine += args[i];\r
24         }\r
25 \r
26         // 標準入力ストリームを取得\r
27         BufferedReader stdReader = new BufferedReader(new InputStreamReader(System.in));\r
28 \r
29         // 出力ファイルの準備\r
30         File out = new File("mail.txt");\r
31         FileOutputStream fo=new FileOutputStream(out);\r
32         PrintStream ps = new PrintStream(fo);\r
33 \r
34         /**\r
35          * 標準入力の内容を、出力ファイルに書き出す。\r
36          */\r
37         ps.println(commandLine);\r
38         ps.println();\r
39         ps.println(convBufferedReader(stdReader));\r
40         \r
41         ps.close();\r
42         fo.close();\r
43     }\r
44 \r
45 \r
46     private static String convBufferedReader(BufferedReader reader) throws IOException {\r
47         if (reader == null) {\r
48             return "";\r
49         }\r
50         StringBuffer strbuf = new StringBuffer();\r
51         for (String line; (line = reader.readLine()) != null; strbuf.append(line +"\n"));\r
52         reader.close();\r
53         reader = null;\r
54         return strbuf.toString();\r
55     }\r
56 }\r