Android写本地文件
16lz
2021-01-23
Android写本地文件
- 通过FileWriter
private static final String LOG_DIR = "sdcard/log/"; public static void write(String log) { FileWriter fileWriter; Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()); String dateStr = simpleDateFormat.format(date); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); String fileName = LOG_DIR + dateFormat.format(date) + ".txt "; try { fileWriter = new FileWriter(fileName, true); fileWriter.write(dateStr + ":" + log + "\r\n"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
- 通过FileOutputStream
public static void write(String message) {FileOutputStream fos = null;try { fos = openFileOutput("sdcard/example.txt", Context.MODE_PRIVATE); fos.write(message.getBytes()); fos.close();} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}}
更多相关文章
- 当html从本地文件系统运行时,如何获取的内容?
- 自动同步vps服务器或本地文件的数据到百度网盘
- 怎么javascript读取本地文件中的数据,并显示在html上
- 【python coding 1:网络检测】ping本地文件里的ip地址