Java tutorial
//package com.java2s; import android.content.Context; import android.text.TextUtils; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class Main { public static OutputStream openFileStream(Context context) { return openFileStream(getDirectory(context), getFileName()); } public static OutputStream openFileStream(String directory, String filePath) { if (TextUtils.isEmpty(directory) || TextUtils.isEmpty(filePath)) { return null; } File directoryFile = new File(directory); if (!directoryFile.exists()) { directoryFile.mkdirs(); } File file = new File(directory + File.separator + filePath); try { if (!file.exists()) { file.createNewFile(); } return new FileOutputStream(file, true); } catch (Exception ex) { ex.printStackTrace(); return null; } } public static String getDirectory(Context context) { return context.getExternalCacheDir() + File.separator + "NightMode"; } public static String getFileName() { return "resultFile.txt"; } }