Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; public class Main { public static OutputStream openCacheFileForOutput(File root, String... path) throws FileNotFoundException { return new BufferedOutputStream(new FileOutputStream(openPath(true, root, path))); } public static File openPath(boolean createPath, File root, String... path) { File f = root; for (int i = 0; i < path.length; i++) { String component = path[i]; if (i == path.length - 1) { // This is the file component so now we create parent directories if (createPath) { f.mkdirs(); } } f = new File(f, component); } return f; } }