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 Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;//from www  . j a  v a  2s  .c om
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

static public void copyFileFromAssets(Context context, String file, String dest) throws Exception {
    InputStream in = null;/*from   ww w.  ja v a 2s . com*/
    OutputStream fout = null;
    int count = 0;

    try {
        in = context.getAssets().open(file);

        File hydappdir = new File("/data/data/" + context.getPackageName() + "/hydapp");
        hydappdir.mkdirs();

        fout = new FileOutputStream(new File(dest));

        byte data[] = new byte[1024];
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static void makedirs(File file) throws IOException {
    checkFile(file);/*from ww  w .  j a  v  a2s.  c om*/
    File parentFile = file.getParentFile();
    if (parentFile != null) {
        if (!parentFile.exists() && !parentFile.mkdirs()) {
            throw new IOException("Creating directories " + parentFile.getPath() + " failed.");
        }
    }
}

From source file:com.ALC.SC2BOAserver.util.SC2BOAserverFileUtil.java

/**
 * This method extracts data to a given directory.
 * /*w w w  .j a  v a2s . co m*/
 * @param directory the directory to extract into
 * @param zipIn input stream pointing to the zip file
 * @throws ArchiveException
 * @throws IOException
 * @throws FileNotFoundException
 */

public static void extractZipToDirectory(File directory, InputStream zipIn)
        throws ArchiveException, IOException, FileNotFoundException {

    ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("zip", zipIn);
    while (true) {
        ZipArchiveEntry entry = (ZipArchiveEntry) in.getNextEntry();
        if (entry == null) {
            in.close();
            break;
        }
        //Skip empty files
        if (entry.getName().equals("")) {
            continue;
        }

        if (entry.isDirectory()) {
            File file = new File(directory, entry.getName());
            file.mkdirs();
        } else {
            File outFile = new File(directory, entry.getName());
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(outFile);
            IOUtils.copy(in, out);
            out.flush();
            out.close();
        }
    }
}

From source file:com.google.caliper.runner.ParsedOptionsTest.java

private static void makeTestVmTree(File baseDir) throws IOException {
    File bin = new File(baseDir, "testVm/bin");
    bin.mkdirs();
    File java = new File(bin, "java");
    Files.touch(java);//from  w  ww.j  a  v  a2s .  c  o m
}

From source file:Main.java

public static void writeFile(String directoryPath, String fileName, String content) throws IOException {

    if (notEmptyStr(directoryPath) && notEmptyStr(fileName)) {

        FileOutputStream fos = null;

        try {//  www .j a  va  2  s  . com
            File directory = new File(directoryPath);

            if (!directory.exists()) {
                directory.mkdirs();
            }
            File file = new File(directoryPath + File.separator + fileName);
            fos = new FileOutputStream(file);
            byte[] contentBytes = content.getBytes("UTF-8");
            fos.write(XML_HEADER.getBytes("UTF-8"));
            fos.write(contentBytes);

        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

From source file:Main.java

/**
 * Save image to the SD card//from w ww .  ja va  2 s. c  o  m
 * 
 * @param photoBitmap
 * @param photoName
 * @param path
 */
public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
    if (checkSDCardAvailable()) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File photoFile = new File(path, photoName + ".png");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(photoFile);
            if (photoBitmap != null) {
                if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                    fileOutputStream.flush();
                    // fileOutputStream.close();
                }
            }
        } catch (FileNotFoundException e) {
            photoFile.delete();
            e.printStackTrace();
        } catch (IOException e) {
            photoFile.delete();
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:cereal.examples.pojo.PojoPersonTest.java

@BeforeClass
public static void start() throws IOException, InterruptedException {
    File target = new File(System.getProperty("user.dir") + "/target");
    assertTrue(target.exists());//from w  w w.j  a  va  2  s.c  o  m
    assertTrue(target.isDirectory());
    File macParent = new File(target, "minicluster");
    macParent.mkdirs();
    File macDir = new File(macParent, PojoPersonTest.class.getName());
    if (macDir.exists()) {
        FileUtils.deleteQuietly(macDir);
    }
    MiniAccumuloConfig cfg = new MiniAccumuloConfig(macDir, PASSWORD);
    cfg.setNumTservers(1);
    mac = new MiniAccumuloCluster(cfg);
    mac.start();
}

From source file:Main.java

public static File getExternalCacheDir(Context context, String dirName) {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Charismatic_yichang"), "data");
    File appCacheDir2 = new File(new File(dataDir, context.getPackageName()), "cache");
    File appCacheDir = new File(appCacheDir2, dirName);
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            Log.w(TAG, "Unable to create external cache directory");
            return null;
        }/*from   ww w.  j av a2s.  c o  m*/
        try {
            new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {
            Log.i(TAG, "Can't create \".nomedia\" file in application external cache directory");
        }
    }
    return appCacheDir;
}

From source file:Main.java

private static File getExternalCacheDir(Context context) {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
    File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            return null;
        }/*from  w  w w  .ja  va  2  s  .  c o  m*/
        try {
            new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {
        }
    }
    return appCacheDir;
}