Android创建文件夹及文件并写入数据
16lz
2022-04-24
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 blocke.printStackTrace();try {bw.close();fw.close();} catch (IOException e1) {// TODO Auto-generated catch block}}}}
更多相关文章
- android 通过pull解析xml文件
- Android中音频文件的使用
- Android(安卓)反编译,修改,重新打包,签名
- Android打电话,挂电话
- android 用Pull 读取XML
- Android播放ogg声音文件
- [Android] ImageButton | Button | TextView 点击和触摸效果实现
- Android遍历SqlLite cursor对象:
- NPM 和webpack 的基础使用