Example usage for java.io File getParentFile

List of usage examples for java.io File getParentFile

Introduction

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

Prototype

public File getParentFile() 

Source Link

Document

Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:Main.java

public static void deleteSinglePicture(Context ctx, String path) {
    String params[] = new String[] { path };
    ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            MediaStore.Images.Media.DATA + " LIKE ?", params);
    File file = new File(path);
    File pFile = file.getParentFile();
    if (pFile != null && pFile.isDirectory()) {
        File files[] = pFile.listFiles();
        if (files == null || files.length == 0) {
            pFile.delete();//from  ww w  . j  av  a2s . com
        }
    }
}

From source file:Main.java

public static boolean CreateDir(String dir) {
    boolean isSuccess = false;
    File file = new File(dir);
    File parentFile = file.getParentFile();
    if (parentFile != null) {
        if (!parentFile.exists()) {
            isSuccess = file.getParentFile().mkdirs();
        } else {/*from www .j  a  v a2 s. com*/
            isSuccess = true;
        }
    }
    return isSuccess;
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*ww w .  j  a  va 2s. c  o  m*/
    } else {
        file.delete();
    }

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:StreamsUtils.java

public static void inputStream2File(InputStream stream, File f) throws IOException {
    f.getParentFile().mkdirs();
    FileOutputStream out = new FileOutputStream(f);
    inputStream2OutputStream(stream, out);
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();/*from   w w  w.ja v  a  2s.  com*/

    if (file != null && file.exists())
        file.delete();

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static void saveFile(final String fileName, final String str) {
    try {/*from w ww  . ja  v  a 2  s .  c o  m*/
        File f = new File(fileName);
        if (new File(f.getParent()).exists() == false) {
            f.getParentFile().mkdirs();
        }
        f.createNewFile();
        PrintStream p = new PrintStream(new FileOutputStream(f, false));
        p.println(str);
        p.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(fileName);
    }
}

From source file:io.github.bunnyblue.droidfix.classcomputer.gradleImpl.GradleImpl15.java

public static void extract() {
    File srcDir = new File(Configure.getInstance().getProguardJarFolder());
    Collection<File> jars = FileUtils.listFiles(srcDir, new String[] { "jar" }, true);
    List<File> jarsList = (List<File>) jars;
    File jar = jarsList.get(0);
    String extractClasses = jar.getParentFile().getAbsolutePath() + File.separator
            + jar.getName().substring(0, jar.getName().indexOf(".jar"));
    Configure.getInstance().setTransformedClassDir(extractClasses);
    File targetFile = new File(extractClasses);
    try {// www . ja v a2  s. com
        FileUtils.deleteDirectory(targetFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ZipUtil.unpack(jar, targetFile);
}

From source file:Main.java

public static boolean writeString(String filePath, String content) {
    File file = new File(filePath);
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();/*from w  w  w  . j ava2  s .  c  o  m*/

    FileWriter writer = null;
    try {

        writer = new FileWriter(file);
        writer.write(content);

    } catch (IOException e) {
    } finally {
        try {
            if (writer != null) {

                writer.close();
                return true;
            }
        } catch (IOException e) {
        }
    }
    return false;
}

From source file:Main.java

public static void savePic(File targetFile, Bitmap bitmap) throws IOException {

    if (!targetFile.getParentFile().exists()) {
        targetFile.getParentFile().mkdirs();
    } else if (!targetFile.exists()) {
        targetFile.createNewFile();// www  . java 2  s  . c  o  m
    } else {
        FileOutputStream fos = new FileOutputStream(targetFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    }
}

From source file:Main.java

public static File saveStreamToFile(InputStream inputStream, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();/*w  w  w. j av a  2s. c  o  m*/
    }
    try {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(path);

        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}