1public class CreateFiles { 2 3 String filenameTemp = Info.audioPath + "/hhaudio" + ".txt"; 4 5 // 创建文件夹及文件 6 public void CreateText() throws IOException { 7 File file = new File(Info.audioPath); 8 if (!file.exists()) { 9 try { 10 // 按照指定的路径创建文件夹 11 file.mkdirs(); 12 } catch (Exception e) { 13 // TODO: handle exception 14 } 15 } 16 File dir = new File(filenameTemp); 17 if (!dir.exists()) { 18 try { 19 // 在指定的文件夹中创建文件 20 dir.createNewFile(); 21 } catch (Exception e) { 22 } 23 } 24 25 } 26 27 // 向已创建的文件中写入数据 28 public void print(String str) { 29 FileWriter fw = null; 30 BufferedWriter bw = null; 31 String datetime = ""; 32 try { 33 SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " 34 + "hh:mm:ss"); 35 datetime = tempDate.format(new java.util.Date()).toString(); 36 fw = new FileWriter(filenameTemp, true);// 37 // 创建 FileWriter 对象,用来写入字符流 38 bw = new BufferedWriter(fw); // 将缓冲对文件的输出 39 String myreadline = datetime + "[]" + str; 40 41 bw.write(myreadline + "\n"); // 写入文件 42 bw.newLine(); 43 bw.flush(); // 刷新该流的缓冲 44 bw.close(); 45 fw.close(); 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 try { 50 bw.close(); 51 fw.close(); 52 } catch (IOException e1) { 53 // TODO Auto-generated catch block 54 } 55 } 56 } 57}
Android 创建文件
Stella981
2021-10-11
1302 0 0
点赞
收藏
评论区
加载中...