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 synchronized void writeToFile(String rootPath, String filename, String data) {

    File file = new File(rootPath);
    if (!file.exists()) {
        file.mkdirs();

    }/* w  w w . j  ava2 s.  com*/

    FileOutputStream fOut = null;
    try {

        File savedfile = new File(rootPath + filename);
        savedfile.createNewFile();//create file if not exists
        fOut = new FileOutputStream(savedfile, true); //append content to the end of file
        OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

        outWriter.write(data);

        fOut.flush();
        outWriter.flush();

        fOut.close();
        outWriter.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:Main.java

private static void dirChecker(String destinationPath) {
    File f = new File(destinationPath);
    if (!f.isDirectory()) {
        f.mkdirs();
    }//from  ww  w  .  j  a  v a2 s .co  m
}

From source file:Main.java

/**
 * get the FileOutputStream//from  www .j  ava  2s .  c o m
 * @param filePath the filePath must contain head "/"
 * @return
 */
public static FileOutputStream getFileOutputStream(String filePath) {
    FileOutputStream fouts = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filePath.trim());

    if (file.exists() && file.isFile()) {
        try {
            fouts = new FileOutputStream(file);
            Log.d("Ragnarok", "get the fouts path = " + file.getAbsolutePath());
            return fouts;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        try {
            File fileDirs = new File(file.getParent());
            fileDirs.mkdirs();
            //Log.d(LOG_TAG, "make the fileDirs " + fileDirs.getPath());
            //file.createNewFile();   
            //Log.d("Ragnarok", "create a new file name " + file.getName());
            Log.d("Ragnarok", "file path " + file.getAbsolutePath());
            synchronized (file) {
                file.createNewFile();
                fouts = new FileOutputStream(file);
                return fouts;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    return null;
}

From source file:Main.java

public static boolean writeToSdcard(byte[] data, String path, String fileName) {
    FileOutputStream fos = null;//from  w  w  w . j  a v  a  2  s  .co m
    try {
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        File file = new File(path + fileName);
        if (file.exists()) {
            file.delete();
        }
        fos = new FileOutputStream(file);
        fos.write(data);
        fos.flush();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void copyFolder(Context context, String assetPath, String sdcardPath) {
    try {/*from w  w  w  . j  a  v a 2s.  com*/
        String[] list = context.getAssets().list(assetPath);

        File f = new File(sdcardPath);
        if (!f.exists())
            f.mkdirs();

        for (String s : list)
            copyFile(context, assetPath + "/" + s, sdcardPath + "/" + s);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static File createApiCacheDir(Context context) {
    File cache = new File(context.getApplicationContext().getCacheDir(), API_CACHE);
    if (!cache.exists()) {
        cache.mkdirs();
    }//from  w  w w  . j a  v a  2  s.co m
    return cache;
}

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {/*  w  ww. ja  v a  2 s . co m*/
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static File savebitmap(Bitmap bitmap) {
    OutputStream outStream = null;
    File folder = new File(Environment.getExternalStorageDirectory().toString() + "/InSyte");
    folder.mkdirs();
    String mDirectory = folder.toString();
    File file = new File(mDirectory, "IMG_" + System.currentTimeMillis() + ".jpg");
    try {//from w  w w .  j av a2 s .c  o m
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;

}

From source file:Main.java

public static void copyAssets(Context context, String dir, String fileName) {
    // String[] files;
    File mWorkingPath = new File(dir);
    if (!mWorkingPath.exists()) {
        if (!mWorkingPath.mkdirs()) {
        }/*ww  w .  jav a 2s  . c om*/
    }
    try {
        InputStream in = context.getAssets().open(fileName);
        System.err.println("");
        File outFile = new File(mWorkingPath, fileName);
        OutputStream out = new FileOutputStream(outFile);
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:Main.java

public static boolean mkdir(String path) {
    File file = new File(path);
    if (file.exists() && file.isDirectory()) {
        return true;
    }//from   www. j a v a 2 s  . co m

    file.mkdirs();
    return true;
}