1 package jp.sourceforge.gokigen.memoma.fileio;
3 import android.os.Environment;
4 import android.util.Log;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.text.SimpleDateFormat;
11 import java.util.Calendar;
13 import jp.sourceforge.gokigen.memoma.Main;
16 * 外部ストレージにデータを記録するために使うユーティリティ
20 public class ExternalStorageFileUtility
22 private final int COPY_BUFFER_SIZE = 32768;
23 private final int BUFFER_MARGIN = 4;
25 private String baseDirectory = "/";
31 public ExternalStorageFileUtility(String offsetDir)
33 /** ベースディレクトリの作成 (あれば何もしない) **/
34 prepareBaseDirectory(offsetDir);
41 private boolean prepareBaseDirectory(String offsetDir)
43 String gokigenDirectory = Environment.getExternalStorageDirectory().getPath() + "/Gokigen";
46 File baseDir = new File(gokigenDirectory);
47 if (baseDir.exists() == false)
49 if (baseDir.mkdirs() == false)
51 // ベースディレクトリ作成失敗...終了する
52 baseDirectory = Environment.getExternalStorageDirectory().getPath();
57 gokigenDirectory = gokigenDirectory + offsetDir;
59 baseDir = new File(gokigenDirectory);
60 if (baseDir.exists() == false)
62 if (baseDir.mkdirs() == false)
64 // ベースディレクトリ作成失敗...終了する
65 baseDirectory = Environment.getExternalStorageDirectory().getPath() + "/Gokigen";
70 baseDirectory = gokigenDirectory;
75 Log.v(Main.APP_IDENTIFIER, "prepareBaseDirectory() : " + ex.getMessage());
77 baseDirectory = Environment.getExternalStorageDirectory().getPath();
83 * @return 書き込みを行うアプリ用ベースディレクトリ
85 public String getGokigenDirectory()
87 return (baseDirectory);
96 public boolean makeDirectory(String dirName)
98 String makeDir = baseDirectory + "/" + dirName;
101 File dir = new File(makeDir);
102 if (dir.exists() == false)
104 return (dir.mkdirs());
110 Log.v(Main.APP_IDENTIFIER, "makeDirectory() : " + ex.getMessage());
117 * @param fileName ファイル名
118 * @param isAppend 追記モードでオープンするか?
121 public FileOutputStream openFileStream(String fileName, boolean isAppend)
125 String targetName = baseDirectory + "/" + fileName;
126 FileOutputStream fileStream = new FileOutputStream(targetName, isAppend);
131 Log.v(Main.APP_IDENTIFIER, "openFileStream() : " + e.getMessage());
137 * 日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
140 private String decideFileNameWithSpecifiedDate(String fileName, String year, String month, String date)
142 String directory = year;
147 directory = directory + "/" + year + month;
150 makeDirectory(directory);
152 directory = directory + "/" + year + month + date;
155 makeDirectory(directory);
157 return (directory + "/" + year + month + date + "-" + fileName);
161 * 日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
164 public String decideFileNameWithDate(String fileName)
166 Calendar calendar = Calendar.getInstance();
168 SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
169 SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
170 SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
172 String year = yearFormat.format(calendar.getTime());
173 String month = monthFormat.format(calendar.getTime());
174 String date = dateFormat.format(calendar.getTime());
176 return (decideFileNameWithSpecifiedDate(fileName, year, month, date));
180 * 日単位でディレクトリを作成し、そこに書き込むためのファイル名を決定する
183 public String decideFileNameWithSpecifiedDate(String fileName, int year, int month, int day)
185 String yearStr = year + "";
186 String monthStr = "";
189 monthStr = "0" + month;
193 monthStr = month + "";
204 return (decideFileNameWithSpecifiedDate(fileName, yearStr, monthStr, dayStr));
214 public String decideDateDirectory(int year, int month, int date)
216 String addMonth = "";
218 String directory = baseDirectory + "/" + year + "/";
227 directory = directory + year + addMonth + month + "/" + year + addMonth + month + addDate + date + "/";
232 * ファイルのコピー (kaniFilerから持ってきた...)
234 * @param destFileName コピー先ファイル (full path)
235 * @param srcFileName コピー元ファイル (full path)
237 public boolean copyFile(String destFileName, String srcFileName)
243 InputStream is = null;
244 OutputStream os = null;
246 if (destFileName == srcFileName)
248 // ファイル名が同じだった場合にはコピーを実行しない
254 srcFile = new File(srcFileName);
255 if (srcFile.exists() != true)
257 // ファイルが存在しなかった、、、終了する
260 is = new FileInputStream(srcFile);
262 long dataFileSize = srcFile.length();
263 byte[] buffer = new byte[COPY_BUFFER_SIZE + BUFFER_MARGIN];
265 dstFile = new File(destFileName);
266 if (dstFile.exists() == true)
268 // ファイルが存在した、、、削除して作り直す
272 os = new FileOutputStream(dstFile);
273 if ((is != null)&&(os != null))
275 while (dataFileSize > 0)
277 int size = is.read(buffer, 0, COPY_BUFFER_SIZE);
282 os.write(buffer, 0, size);