Java tutorial
//package com.java2s; //License from project: Apache License import android.util.*; import java.io.*; public class Main { public static void writeContentToFile(String fileName, String contents) throws IOException { Log.d("writeContentToFile", fileName); File f = new File(fileName); f.getParentFile().mkdirs(); File tempFile = new File(fileName + ".tmp"); FileWriter fw = new FileWriter(tempFile); BufferedWriter bw = new BufferedWriter(fw); int length = contents.length(); if (length > 0) { bw.write(contents); // int apart = Math.min(length, 65536); // int times = length / apart; // for (int i = 0; i < times; i++) { // bw.write(contents, i * apart, apart); // } // if (length % apart != 0) { // bw.write(contents, times * apart, length - times * apart); // } bw.flush(); fw.flush(); bw.close(); fw.close(); f.delete(); tempFile.renameTo(f); } } public static void close(Closeable closable) { if (closable != null) { try { closable.close(); } catch (IOException e) { e.printStackTrace(); } } } public static boolean delete(File file) { file.setWritable(true); try { if (!file.delete()) { FileOutputStream fos = new FileOutputStream(file); fos.write(0); fos.flush(); fos.close(); } Log.d("delete", "Deleted file: " + file + " successfully"); return true; } catch (IOException e) { Log.d("delete", "The deleting file: " + file + " is not successfully", e); return false; } } private static void delete(File file, StringBuilder sb) { long length = file.length(); boolean deleted = file.delete(); if (deleted) { sb.append(file.getAbsolutePath() + " length " + length + " bytes, deleted.\r\n"); } else { sb.append(file.getAbsolutePath() + " length " + length + " bytes, can't delete.\r\n"); } } }