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

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file//from  w  ww  .  j  av  a2  s .co  m
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}

From source file:Main.java

public static String getHandleDirPath(String path) {
    File file = new File(path);
    if (file.exists()) {

    } else {//  w w  w .  j a  v  a2s . c  o m
        file.getParentFile().mkdirs();
    }
    return path;
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bmp, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*from  w ww.ja  v a 2 s  .co  m*/
    }
    if (file.exists()) {
        file.delete();
    }
    try {

        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.flush();
        out.close();
        Log.i("saveBitmap success:", path);
        return true;
    } catch (Exception e) {
        Log.e("saveBitmap:" + path, e.toString());
    }
    return false;

}

From source file:com.baidu.terminator.manager.common.file.FileUtils.java

public static void createFile(String fileLocation) {
    File file = new File(fileLocation);
    File parentFile = file.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();/*from w  ww. ja  va  2  s .c  om*/
    }
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            LOGGER.error("create file :" + fileLocation + " fail!");
        }
    }
}

From source file:Main.java

public static Boolean createFile(String path) {
    File file = new File(path);
    File parantFile = file.getParentFile();
    if (null != parantFile && !parantFile.exists() && !parantFile.mkdirs()) {
        return false;
    }/*w ww.  j a v  a2  s .  com*/

    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();//w  ww .j  ava2  s  .  c  o m
    } else {
        destFile.delete();
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

public static void saveBArrToFile(String fileName, byte[] barr) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(barr, 0, barr.length);/*from  w  w w .  jav a 2s. co  m*/
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:Main.java

public static void touch(File file) throws IOException {
    if (!file.exists()) {
        File parent = file.getParentFile();
        if (parent != null)
            if (!parent.exists())
                if (!parent.mkdirs())
                    throw new IOException("Cannot create parent directories for file: " + file);
        file.createNewFile();/*from   ww  w  .j a  v a 2 s. com*/
    }
}

From source file:biz.paluch.maven.configurator.FileTemplating.java

private static File getTargetFile(File next) {
    File parent = next.getParentFile();

    String filename = next.getName();
    filename = filename.replaceAll("\\.template", "");
    return new File(parent, filename);
}

From source file:Main.java

public static <K, V> void printMap(Map<K, V> A, File file)
        throws FileNotFoundException, UnsupportedEncodingException {
    file.getParentFile().mkdirs();
    PrintWriter p = new PrintWriter(file, "UTF-8");
    for (K k : A.keySet())
        p.println(k + "\t" + A.get(k));
    p.close();/*from  w ww .  ja va  2 s . c  om*/
}