Android 建立文件

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  
            }  
        }  
    }  
}
相關文章
相關標籤/搜索