Android写本地文件

    技术2022-07-10  142

    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(); } }
    Processed: 0.010, SQL: 9