package elwin.fei.mobileaudio;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class CreateFiles {
String filenameTemp = Info.audioPath + "/hhaudio" + ".txt";
//建立資料夾及檔案
public void CreateText() throws IOException {
File file = new File(Info.audioPath);
if (!file.exists()) {
try {
//按照指定的路徑建立資料夾
file.mkdirs();
} catch (Exception e) {
// TODO: handle exception
}
}
File dir = new File(filenameTemp);
if (!dir.exists()) {
try {
//在指定的資料夾中建立檔案
dir.createNewFile();
} catch (Exception e) {
}
}
}
//向已建立的檔案中寫入資料
public void print(String str) {
FileWriter fw = null;
BufferedWriter bw = null;
String datetime = "";
try {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " "
+ "hh:mm:ss");
datetime = tempDate.format(new java.util.Date()).toString();
fw = new FileWriter(filenameTemp, true);//
// 建立FileWriter物件,用來寫入字元流
bw = new BufferedWriter(fw); // 將緩衝對檔案的輸出
String myreadline = datetime + "[]" + str;
bw.write(myreadline + "\n"); // 寫入檔案
bw.newLine();
bw.flush(); // 重新整理該流的緩衝
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
bw.close();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}
}
}