Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static boolean writeStringToFile(String fileName, String content) { File file = new File(fileName); if (content == null) return false; if (!file.exists()) { String filepath = file.getAbsolutePath(); String path = getParentPath(filepath); File dir = new File(path); dir.mkdirs(); } FileOutputStream fos = null; byte[] data; try { fos = new FileOutputStream(file, false); data = content.getBytes(); fos.write(data, 0, data.length); fos.flush(); return true; } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != fos) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } data = null; fos = null; } return false; } public static String getParentPath(final String path) { if (path == null) return null; String parenPath = null; int idx = path.lastIndexOf("/"); if (idx != -1 && idx != 0) parenPath = path.substring(0, idx); return parenPath; } }