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:Main.java

public static File getDirWithCreate(String path) throws IOException {
    File dir = new File(path);

    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            throw new IOException("Can not create directory " + path);
        }//ww  w . ja v  a2 s .co  m
    }

    return dir;
}

From source file:Main.java

/**
 * Write the given bitmap to a file in the external storage. Requires 
 * "android.permission.WRITE_EXTERNAL_STORAGE" permission.
 *//*from  ww w .jav  a  2  s  .c  o  m*/
public static void writeToFile(Bitmap bitmap, String dir, String filename)
        throws FileNotFoundException, IOException {
    File sdCard = Environment.getExternalStorageDirectory();
    File dirFile = new File(sdCard.getAbsolutePath() + "/" + dir);
    dirFile.mkdirs();
    File f = new File(dirFile, filename);
    FileOutputStream fos = new FileOutputStream(f, false);
    bitmap.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, fos);
    fos.flush();
    fos.close();
}

From source file:Main.java

/**
 * Make directories. If directory exists, do nothing.
 *
 * @param context/*from   w ww.j  ava 2s. c om*/
 * @param path
 *          directory path to create.
 * @throws java.io.IOException
 *           if directory doesn't exist and fail to create.
 */
public static void mkdirs(Context context, String path) throws IOException {
    Resources res = context.getResources();
    File pathFile = new File(path);
    if (!pathFile.exists()) {
        if (!pathFile.mkdirs()) {
            throw new IOException(String.format(FAIL_CREATE_DIRECTORY, pathFile.getAbsolutePath()));
        }
    }
}

From source file:Main.java

public static File getDir(File parent, String dirName) {
    File file = new File(parent, dirName);
    if (!file.exists() || !file.isDirectory()) {
        file.mkdirs();
    }//  www.j  a  v a 2 s .c  o m
    return file;
}

From source file:Main.java

/**
 * Returns the music cache directory./* w  ww  . j ava 2 s  . c om*/
 * @return Music cache directory
 */
public static File getMusicCacheDir() {
    File music = Environment.getExternalStoragePublicDirectory("SismicsMusic");
    File cache = new File(music, "cache");
    if (!cache.exists()) {
        cache.mkdirs();
    }
    return cache;
}

From source file:Main.java

public static File createFileInSDCard(String path, String fileName) {
    File dir = new File(path);
    try {//from   ww  w  .  ja va2 s.com
        if (!dir.exists() && dir.mkdirs()) {
            System.out.println("Directory created");
        } else {
            System.out.println("Directory is not created");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    File file = null;
    try {
        if (dir.exists()) {
            file = new File(dir, fileName);
            file.createNewFile();
        } else {

        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return file;
}

From source file:Main.java

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();
    }//from   w w w .  j  av a 2  s  . co m
    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;
}

From source file:Main.java

public static long getFreeSpace(String path) {
    File file = new File(path);
    if (!(file.isDirectory() && file.exists())) {
        file.mkdirs();
        if (file.isDirectory()) {
            return file.getFreeSpace();
        } else {//from www .  j  av  a 2s .  c o  m
            return 0L;
        }
    } else {
        return file.getFreeSpace();
    }
}

From source file:Main.java

public static String saveFile(Bitmap bm, String dirPath, String fileName, int scale) {
    File dirFile = new File(dirPath);
    if (!dirFile.exists()) {
        dirFile.mkdirs();
    }// w  w w  .  j  a  v a  2s  . c  o m
    File myCaptureFile = new File(dirPath + fileName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, scale, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return dirPath + fileName;
}

From source file:Utils.java

public static boolean buildDirectory(File file) {
    return file.exists() || file.mkdirs();
}