Example usage for java.io File mkdirs

List of usage examples for java.io File mkdirs

Introduction

In this page you can find the example usage for java.io File mkdirs.

Prototype

public boolean mkdirs() 

Source Link

Document

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

Usage

From source file:ch.zhaw.iamp.rct.App.java

private static void ensureConfigDirectoryExists() {
    File configDirectory = new File(CONFIG_DIRECTORY);

    if (!configDirectory.exists()) {
        configDirectory.mkdirs();
    }//from   w  w  w  . j  a v a 2s.  c om
}

From source file:Main.java

/**
 * @param context the context//from  w  w  w. ja va2 s . c o m
 * @param filename the filename
 * @return true if a file named "filename" is stored in the downloads directory
 */
public static Boolean doesFileExistInDownloads(Context context, String filename) {
    File dstDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    if (dstDir != null) {
        dstDir.mkdirs();
    }

    File dstFile = new File(dstDir, filename);
    return dstFile.exists();
}

From source file:Main.java

public static void initWidgetOneFile(Context context, String appId) {
    String root = null;//from  ww w. j  av a  2 s. c  om
    appId += "/";
    if (sdCardIsWork()) {
        root = getSdCardRootPath();
    } else {
        root = context.getFilesDir().getAbsolutePath() + "/";
    }
    String[] fileDir = { root + F_APP_PATH + appId, root + F_WIDGET_PATH,
            root + F_APP_PATH + appId + F_APP_VIDEO, root + F_APP_PATH + appId + F_APP_PHOTO,
            root + F_APP_PATH + appId + F_APP_AUDIO, root + F_APP_PATH + appId + F_APP_MYSPACE };
    int size = fileDir.length;
    for (int i = 0; i < size; i++) {
        File file = new File(fileDir[i]);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    String noMediaStr = root + F_BASE_WGT_PATH + ".nomedia";
    File noMedia = new File(noMediaStr);
    if (!noMedia.exists()) {
        try {
            noMedia.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Creates a new and empty directory in the default temp directory using the
 * given prefix. This methods uses {@link File#createTempFile} to create a
 * new tmp file, deletes it and creates a directory for it instead.
 * //from  w ww.  j a  v  a 2 s  .  co  m
 * @param prefix The prefix string to be used in generating the diretory's
 * name; must be at least three characters long.
 * @return A newly-created empty directory.
 * @throws IOException If no directory could be created.
 */
public static File createTempDir(String prefix) throws IOException {
    String tmpDirStr = System.getProperty("java.io.tmpdir");
    if (tmpDirStr == null) {
        throw new IOException("System property 'java.io.tmpdir' does not specify a tmp dir");
    }

    File tmpDir = new File(tmpDirStr);
    if (!tmpDir.exists()) {
        boolean created = tmpDir.mkdirs();
        if (!created) {
            throw new IOException("Unable to create tmp dir " + tmpDir);
        }
    }

    File resultDir = null;
    int suffix = (int) System.currentTimeMillis();
    int failureCount = 0;
    do {
        resultDir = new File(tmpDir, prefix + suffix % 10000);
        suffix++;
        failureCount++;
    } while (resultDir.exists() && failureCount < 50);

    if (resultDir.exists()) {
        throw new IOException(
                failureCount + " attempts to generate a non-existent directory name failed, giving up");
    }
    boolean created = resultDir.mkdir();
    if (!created) {
        throw new IOException("Failed to create tmp directory");
    }

    return resultDir;
}

From source file:Main.java

public static File getArtworkStorageDir() {
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),
            ARTWORKS_DIR_NAME);/*from   w  ww .j  av  a  2  s.c o  m*/
    if (!file.mkdirs()) {
        Log.e(TAG, "Directory not created");
    }
    return file;
}

From source file:Main.java

public static boolean writeFile(String filePath, String fileName, String content, boolean append) {
    FileWriter fileWriter = null;
    boolean result = false;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        try {//from  w w w  . ja v  a 2  s. c  o  m

            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Log.i("file", filePath);
            fileWriter = new FileWriter(filePath + fileName, append);
            fileWriter.write(content);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("file", e.toString());
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    return result;
}

From source file:Main.java

private static void fileUnZip(ZipInputStream zis, File file) throws FileNotFoundException, IOException {
    java.util.zip.ZipEntry zip = null;
    while ((zip = zis.getNextEntry()) != null) {
        String name = zip.getName();
        File f = new File(file.getAbsolutePath() + File.separator + name);
        if (zip.isDirectory()) {
            f.mkdirs();
        } else {/* w w  w  .jav a  2 s. com*/
            f.getParentFile().mkdirs();
            f.createNewFile();
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(f));
                byte b[] = new byte[2048];
                int aa = 0;
                while ((aa = zis.read(b)) != -1) {
                    bos.write(b, 0, aa);
                }
                bos.flush();
            } finally {
                bos.close();
            }
            bos.close();
        }
    }

}

From source file:com.smartitengineering.cms.binder.guice.InjectionTest.java

@BeforeClass
public static void setUpTests() throws Exception {
    Initializer.init();/*from w ww  .  j a  va  2 s  .  co  m*/
    try {
        File snapDir = new File("./target/zk/");
        snapDir.mkdirs();
        ZooKeeperServer server = new ZooKeeperServer(snapDir, snapDir, 2000);
        standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(CLIENT_PORT));
        standaloneServerFactory.startup(server);
        if (!AppTest.waitForServerUp(CLIENT_PORT, CONNECTION_TIMEOUT)) {
            throw new IOException("Waiting for startup of standalone server");
        }
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();
        }/*ww  w.j av a 2  s .c o  m*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:net.pocketpixels.hubmanager.DBmanager.java

/**
 *Loads all the objects of class type out of JSON from the given folder
 * @param type The class of the objects to be loaded
 * @param loc the location of the folder to load the data from
 * @return returns a HashMap of the file names and the Objects loaded
 *///from w  w  w  .  j a va  2  s  .c  o  m
public static HashMap<String, Object> loadAllObj(Class type, File loc) {
    if (!loc.exists()) {
        loc.mkdirs();
        return null;
    }
    HashMap<String, Object> rtn = new HashMap<String, Object>();
    for (File f : loc.listFiles()) {
        rtn.put(f.getName(), loadObj(type, f));
    }
    return rtn;
}