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 saveObj(Object obj, String fileName) throws IOException {
    File f = new File(fileName + ".tmp");
    f.getParentFile().mkdirs();
    FileOutputStream fos = new FileOutputStream(f);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(obj);/*w  w  w.  j ava2 s .  c o m*/
    out.flush();
    out.close();
    fos.flush();
    fos.close();
    File file = new File(fileName);
    file.delete();
    f.renameTo(file);
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, String file, CompressFormat format) {
    OutputStream os = null;/* w w w .  j  a va  2s  . c o  m*/
    try {
        File f = new File(file);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        os = new BufferedOutputStream(new FileOutputStream(file));
        if (bitmap.compress(format, 100, os)) {
            os.flush();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    return false;
}

From source file:Main.java

public static void createFile(File file, int numBytes) throws IOException {
    File parentFile = file.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();//w ww. ja va  2 s. c om
    }
    byte[] buffer = new byte[numBytes];
    FileOutputStream output = new FileOutputStream(file);
    try {
        output.write(buffer);
    } finally {
        output.close();
    }
}

From source file:it.unibas.spicybenchmark.persistence.DAOLogFile.java

public static void saveLog(String log, String logFile) throws DAOException {
    BufferedWriter writer = null;
    try {// w ww . java 2  s .  c  o m
        File f = new File(logFile);
        f.getParentFile().mkdirs();
        FileWriter fileWriter = new FileWriter(logFile);
        writer = new BufferedWriter(fileWriter);
        writer.write(log);
    } catch (FileNotFoundException fnfe) {
        throw new DAOException(" File not found: " + fnfe);
    } catch (IOException ioe) {
        throw new DAOException(" Error: " + ioe);
    } catch (NoSuchElementException nse) {
        throw new DAOException(" Error in file format: " + nse);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:Main.java

public static boolean OutPutImage(File file, Bitmap bitmap) {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*from w ww  .j a  va  2s  .c  o m*/
    }
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        if (bos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            fos.flush();
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static void save(String paramString, Document paramDocument) throws Exception {
    DOMSource localDOMSource = new DOMSource(paramDocument);
    File localFile1 = new File(paramString);
    File localFile2 = localFile1.getParentFile();
    localFile2.mkdirs();/* ww w .ja  v a  2s. c o  m*/
    StreamResult localStreamResult = new StreamResult(localFile1);
    try {
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        Properties localProperties = localTransformer.getOutputProperties();
        localProperties.setProperty("encoding", "UTF-8");
        localProperties.setProperty("indent", "yes");
        localTransformer.setOutputProperties(localProperties);
        localTransformer.transform(localDOMSource, localStreamResult);
    } catch (TransformerConfigurationException localTransformerConfigurationException) {
        localTransformerConfigurationException.printStackTrace();
    } catch (TransformerException localTransformerException) {
        localTransformerException.printStackTrace();
    }
}

From source file:Main.java

public static void saveISToFile(InputStream is, String fileName) 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);
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] barr = new byte[32768];
    int read = 0;
    while ((read = bis.read(barr)) > 0) {
        bos.write(barr, 0, read);/*from   ww  w. j a v  a 2  s  .c  om*/
    }
    bis.close();
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:Main.java

public static boolean createFile(String filePath) {
    try {//  w  ww.  java2s .c  om
        File file = new File(filePath);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

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

From source file:Main.java

public static void Unzip(String dir, byte[] data) throws Exception {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ZipInputStream zipIn = new ZipInputStream(input);
    ZipEntry entry;/* w  w w. j av a 2s  . co m*/
    while ((entry = zipIn.getNextEntry()) != null) {
        String outpath = dir + entry.getName();
        FileOutputStream output = null;
        try {
            File f = new File(outpath);
            f.getParentFile().mkdirs();

            output = new FileOutputStream(f);
            int len = 0;
            byte[] buffer = new byte[4096];
            while ((len = zipIn.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

    zipIn.close();
}

From source file:Main.java

public static void writeContatctInfo(String accountId, Vector contactInfo, File contactInfoFile)
        throws IOException {
    contactInfoFile.getParentFile().mkdirs();
    FileOutputStream contactFileOutputStream = new FileOutputStream(contactInfoFile);
    DataOutputStream out = new DataOutputStream(contactFileOutputStream);
    out.writeUTF((String) contactInfo.get(0));
    out.writeInt(((Integer) (contactInfo.get(1))).intValue());
    for (int i = 2; i < contactInfo.size(); ++i) {
        out.writeUTF((String) contactInfo.get(i));
    }//from  w  w  w  .jav a2 s .c o  m
    out.close();
}