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 writeFile(InputStream in, File file) throws IOException {
    Log.d(TAG, "write file=====start==");
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();//from w  w  w . j a v  a  2 s.c o m

    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);
    }
    Log.d(TAG, "write file====success===");
    out.flush();
    out.close();
    in.close();

}

From source file:Main.java

public static boolean create(File file) throws IOException {
    if (file.exists()) {
        return true;
    }/*from  w  w w.  j a v  a  2 s  .  c o m*/

    File parent = file.getParentFile();
    parent.mkdirs();
    return file.createNewFile();
}

From source file:Main.java

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file//from   ww  w.ja v  a2  s .c o  m
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    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 file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:Main.java

public static boolean copy(String srcFile, String dstFile) {
    FileInputStream fis = null;//from   www  .ja v  a  2  s . c  o m
    FileOutputStream fos = null;
    try {

        File dst = new File(dstFile);
        if (!dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        }

        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(dstFile);

        byte[] buffer = new byte[1024];
        int len = 0;

        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    return true;
}

From source file:Main.java

public static boolean canWriteOrCreate(File file) {
    if (file.exists()) {
        return file.canWrite();
    }/*  w w  w  .  j  a  va2  s. co  m*/
    File parent = file.getParentFile();
    return parent != null && parent.exists() && parent.canWrite();
}

From source file:Main.java

public static void copyFile(File srcFile, File desFile) throws FileNotFoundException, IOException {
    try {//from  ww w .  j av a 2s .  c o  m
        File parent = desFile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
    } catch (Exception e) {
    }
    //
    InputStream input = new FileInputStream(srcFile);
    OutputStream output = new FileOutputStream(desFile);
    copyFileStream(input, output);
}

From source file:net.firejack.aws.license.LicenseHelper.java

public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException {
    signature(license);//from   w  ww. j ava 2  s. c o  m

    File tmp = new File("/tmp/", license.getName() + ".xml");

    tmp.getParentFile().mkdirs();
    tmp.createNewFile();
    FileOutputStream stream = new FileOutputStream(tmp);

    JAXBContext jaxbContext = JAXBContext.newInstance(License.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(license, stream);
    stream.close();

    return tmp;
}

From source file:Main.java

public static void saveDocument(Document doc, File file) throws IOException {
    File parentDir = file.getParentFile();
    if ((parentDir != null) && !parentDir.exists()) {
        parentDir.mkdirs();//  w  ww . jav a2  s .com
    }
    writeDocument(doc, new FileOutputStream(file));
}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Unzip.java

public static File unGzip(boolean verbose, File outputDir, String outName, File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException {
    String inFilePath = inputFile.getAbsolutePath();
    if (verbose) {
        System.out.println("unzipping " + inFilePath);
    }/* w w w .  java  2s.  co  m*/
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath));

    File outFile = new File(outputDir, outName);
    outFile.getParentFile().mkdirs();
    String outFilePath = outFile.getAbsolutePath();
    OutputStream out = new FileOutputStream(outFilePath);

    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    gzipInputStream.close();
    out.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    if (verbose) {
        System.out.println("unzipped " + inFilePath);
    }
    return new File(outFilePath);
}

From source file:com.splout.db.common.CompressorUtil.java

public static void uncompress(File file) throws IOException {
    uncompress(file, file.getParentFile());
}