Java tutorial
//package com.java2s; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class Main { public static OutputStream getOutputStream(File dir, String fileName) throws Exception { File outFile = new File(dir, fileName); // as this could be dir=.../maps fileName=preview/file.jpg // we need to make sure the preview dir exists, and if it does not, we must make it File parent = outFile.getParentFile(); if (!parent.isDirectory() && !parent.mkdirs()) { // if it does not exist and i cant make it throw new RuntimeException("can not create dir " + parent); } return new FileOutputStream(outFile); } }