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

/**
 * Store xml Signature represented by dom.Document into xml file
 * @param doc - XML Signature//  www . ja v a 2 s .  c  om
 * @param fileName - name of output file
 */
public static boolean storeSignatureToXMLFile(Document doc, String fileName) {
    boolean stored = false;
    OutputStream os;
    File absolute = new File(fileName);

    String outputPath = new String();
    if (absolute.getParentFile().isAbsolute()) {
        outputPath = fileName; //path is absolute
    } else {
        outputPath = baseDir + File.separator + fileName; //relative, store it into %project_dir%/dist/signatures
    }

    File file = new File(outputPath);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    try {
        os = new FileOutputStream(outputPath);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        //trans.setOutputProperty(OutputKeys.INDENT, "no");
        //trans.setOutputProperty(OutputKeys.METHOD, "xml");

        trans.transform(new DOMSource(doc), new StreamResult(os));
        stored = true;
    } catch (FileNotFoundException e) {
        handleError(e);
    } catch (TransformerConfigurationException e) {
        handleError(e);
    } catch (TransformerException e) {
        handleError(e);
    }
    return stored;
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive.//  w  ww.  jav a 2s.  c om
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:com.github.drbookings.io.Backup.java

public static void make(final File file) {
    if (file.exists() && file.length() != 0) {
        try {/*from w w w  .ja va2s  .c  o m*/
            final File backupFile = new File(file.getParentFile(), file.getName() + ".bak");
            FileUtils.copyFile(file, backupFile);
            if (logger.isInfoEnabled()) {
                logger.info("Backup created as " + backupFile);
            }
        } catch (final IOException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getLocalizedMessage(), e);
            }
        }
    }
}

From source file:com.manning.androidhacks.hack037.MediaUtils.java

public static void saveRaw(Context context, int raw, String path) {
    File completePath = new File(Environment.getExternalStorageDirectory(), path);

    try {/*from   w  w  w.j  a va  2  s  .c  o  m*/
        completePath.getParentFile().mkdirs();
        completePath.createNewFile();

        BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath)));
        BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw));
        byte[] buff = new byte[32 * 1024];
        int len;
        while ((len = bis.read(buff)) > 0) {
            bos.write(buff, 0, len);
        }
        bos.flush();
        bos.close();

    } catch (IOException io) {
        Log.e(TAG, "Error: " + io);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.spelling.experiments.data.util.DataUtil.java

public static Map<String, String> getAllDatasets(String path, String[] extensions) throws IOException {

    Map<String, String> datasetMap = new HashMap<String, String>();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    for (Resource resource : resolver.getResources(path)) {
        for (File datasetFile : FileUtils.listFiles(resource.getFile(), extensions, true)) {
            datasetMap.put(datasetFile.getAbsolutePath(), datasetFile.getParentFile().getName());
        }/* www .j  a  v a 2s . c o  m*/
    }

    return datasetMap;
}

From source file:com.zurich.lifeac.intra.control.FTPUtil.java

public static boolean downloadSingleFile(FTPClient ftpClient, String remoteFilePath, String savePath)
        throws IOException {

    // code to download a file...
    File downloadFile = new File(savePath);

    File parentDir = downloadFile.getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdir();/*w ww.jav  a  2 s . com*/
    }

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
    try {
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        System.out.println("downloadSingle FileremoteFilePath:" + remoteFilePath);
        return ftpClient.retrieveFile(remoteFilePath, outputStream);
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:Main.java

/**
 * simply get binary repository's root according to source repository's root.
 * won't do any check.//from   ww w  . j  av  a 2  s.  c om
 * 
 * @param sourceRepoDir
 * @return
 */
public static File getBinaryRepositoryRoot(File sourceRepoDir) {
    // repository foldername
    String repositoryFolderName = sourceRepoDir.getName();

    // go to parent directory
    File parent = sourceRepoDir.getParentFile();
    return new File(parent, ("." + repositoryFolderName));
}

From source file:Main.java

public static boolean isPathUnderAppName(String appName, File path) {

    File parentDir = new File(getAppFolder(appName));

    while (path != null && !path.equals(parentDir)) {
        path = path.getParentFile();
    }// w  ww. j  ava 2 s.  c  om

    return (path != null);
}

From source file:net.orpiske.sdm.lib.io.IOUtil.java

private static boolean createParentDirectories(File dir) {
    if (!dir.getParentFile().exists()) {
        return dir.getParentFile().mkdirs();
    }/*from  w  ww.  j  a  v  a2 s  . com*/

    return true;
}

From source file:Main.java

public static boolean deleteFile(String path) {
    // If path == null will throw NullPointException.
    if (TextUtils.isEmpty(path)) {
        return false;
    }/*from ww  w.  j  av  a  2 s  . c  o  m*/

    File file = new File(path);
    if (!file.exists()) {
        return false;
    }

    boolean flag = file.delete();
    File parent = file.getParentFile();
    if (isEmptyDirectory(parent)) {
        parent.delete();
    }
    return flag;
}