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:com.googlecode.osde.internal.profiling.Profile.java

private static void unzipFile(File target, InputStream zipStream) throws IOException {
    FileOutputStream out = null;/*from   w ww .  j  ava 2s.c  o  m*/
    try {
        forceMkdir(target.getParentFile());
        out = new FileOutputStream(target);
        copy(zipStream, out);
    } finally {
        closeQuietly(out);
    }
}

From source file:com.binomed.showtime.android.util.CineShowTimeLayoutUtils.java

public static InputStream existFile(String urlImg, String fileName) {
    InputStream stream = null;/* w  w  w.java2s . com*/
    try {
        File root = Environment.getExternalStorageDirectory();
        if (fileName == null) {
            fileName = urlImg.substring(urlImg.lastIndexOf("/"), urlImg.length());
        }

        File posterFile = new File(root,
                new StringBuilder(CineShowtimeCst.FOLDER_POSTER).append(fileName).toString());
        posterFile.getParentFile().mkdirs();
        if (posterFile.exists()) {
            Log.i(TAG, "img existe");
            stream = new FileInputStream(posterFile);
        }

    } catch (IOException e) {
        Log.e(TAG, "Could not write file " + e.getMessage()); //$NON-NLS-1$
    }

    return stream;
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.ExtractionTools.java

private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream)
        throws IOException {
    final int BUFFER_SIZE = 8192;
    ArchiveEntry entry = archiveInputStream.getNextEntry();

    while (entry != null) {
        final File destinationFile = getDestinationFile(destination, entry.getName());

        if (entry.isDirectory()) {
            destinationFile.mkdir();/*from w ww. j  a  v  a 2s. c  o m*/
        } else {
            destinationFile.getParentFile().mkdirs();
            try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
                final byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;

                while ((bytesRead = archiveInputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
            }
        }

        entry = archiveInputStream.getNextEntry();
    }
}

From source file:com.manydesigns.elements.util.ElementsFileUtils.java

public static void moveFileSafely(File tempFile, String fileName) throws IOException {
    File destination = new File(fileName);
    if (!destination.exists()) {
        FileUtils.moveFile(tempFile, destination);
    } else {/*from w w w .  j  a  va2s .c  o  m*/
        File backup = File.createTempFile(destination.getName(), ".backup", destination.getParentFile());
        if (!backup.delete()) {
            logger.warn("Cannot delete: {}", backup);
        }
        FileUtils.moveFile(destination, backup);
        FileUtils.moveFile(tempFile, destination);
        if (!backup.delete()) {
            logger.warn("Cannot delete: {}", backup);
        }
    }
}

From source file:net.shopxx.util.CompressUtils.java

public static void archive(File[] srcFiles, File destFile, String archiverName) {
    Assert.notNull(destFile);/*from w w  w  . j a va2s  .c o m*/
    Assert.state(!destFile.exists() || destFile.isFile());
    Assert.hasText(archiverName);

    File parentFile = destFile.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();
    }
    ArchiveOutputStream archiveOutputStream = null;
    try {
        archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiverName,
                new BufferedOutputStream(new FileOutputStream(destFile)));
        if (ArrayUtils.isNotEmpty(srcFiles)) {
            for (File srcFile : srcFiles) {
                if (srcFile == null || !srcFile.exists()) {
                    continue;
                }
                Set<File> files = new HashSet<File>();
                if (srcFile.isFile()) {
                    files.add(srcFile);
                }
                if (srcFile.isDirectory()) {
                    files.addAll(FileUtils.listFilesAndDirs(srcFile, TrueFileFilter.INSTANCE,
                            TrueFileFilter.INSTANCE));
                }
                String basePath = FilenameUtils.getFullPath(srcFile.getCanonicalPath());
                for (File file : files) {
                    try {
                        String entryName = FilenameUtils.separatorsToUnix(
                                StringUtils.substring(file.getCanonicalPath(), basePath.length()));
                        ArchiveEntry archiveEntry = archiveOutputStream.createArchiveEntry(file, entryName);
                        archiveOutputStream.putArchiveEntry(archiveEntry);
                        if (file.isFile()) {
                            InputStream inputStream = null;
                            try {
                                inputStream = new BufferedInputStream(new FileInputStream(file));
                                IOUtils.copy(inputStream, archiveOutputStream);
                            } catch (FileNotFoundException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } catch (IOException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } finally {
                                IOUtils.closeQuietly(inputStream);
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } finally {
                        archiveOutputStream.closeArchiveEntry();
                    }
                }
            }
        }
    } catch (ArchiveException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }
}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Builds a JAR file from the contents of a directory on the filesystem (recursively).
 * Adapted from stackoverflow solution.  
 * // w  ww. j  a  va 2 s.c  om
 * @see http://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
 * 
 * @param directory the directory containing the content to be xzipped up
 * @param output the zip file to be written to
 */
public static void jar(File directory, File output) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    output.getParentFile().mkdirs();
    JarOutputStream target = new JarOutputStream(new FileOutputStream(output), manifest);
    zip(directory, directory, target);
    target.close();
}

From source file:com.megatome.j2d.support.JavadocSupport.java

/**
 * Find all values to be indexed within the specified list of files.
 * @param filesToIndex List of Javadoc files to parse
 * @return List of relevant values to be indexed in the docset
 * @throws BuilderException/*from  ww  w. j a  v  a 2 s .com*/
 */
public static List<SearchIndexValue> findSearchIndexValues(List<File> filesToIndex) throws BuilderException {
    final List<SearchIndexValue> values = new ArrayList<>();
    for (final File f : filesToIndex) {
        final List<SearchIndexValue> indexValues = indexFile(f);
        values.addAll(indexValues);
        final File parentDir = f.getParentFile();
        for (final SearchIndexValue searchIndexValue : indexValues) {
            if (extraIndexingTypes.contains(searchIndexValue.getType())) {
                final File classFile = getFile(parentDir, searchIndexValue.getPath());
                values.addAll(indexClassFile(classFile));
            }
        }
    }
    return values;
}

From source file:dtool.dub.BundlePath.java

/***
 * Searches for a manifest file in any of the directories denoted by given path, starting in path. 
 *///from  w  ww  .  jav a 2s.  c o m
public static BundlePath findBundleForPath(File path) {
    if (path == null || !path.isAbsolute()) {
        return null;
    }
    BundlePath bundleFile = create(path);
    if (bundleFile != null && bundleFile.getManifestFilePath().exists()) {
        return bundleFile;
    }
    return findBundleForPath(path.getParentFile());
}

From source file:com.asakusafw.shafu.core.util.IoUtils.java

private static File createFile(File base, ArchiveEntry entry, InputStream contents) throws IOException {
    File file = new File(base, entry.getName());
    File parent = file.getParentFile();
    parent.mkdirs();/*w  w w  .jav  a 2  s .  c o m*/
    OutputStream output = new FileOutputStream(file);
    try {
        IOUtils.copy(contents, output);
    } finally {
        output.close();
    }
    return file;
}

From source file:cn.fql.utility.FileUtility.java

/**
 * make a directory for specified file/*w  w  w. j a  v  a  2 s  . c o m*/
 * if directory existed, it is not requried to create a new one
 *
 * @param file specified file<code>java.io.File</code>
 */
public static void createDir(File file) {
    if (!file.exists()) {
        File dir = file.getParentFile();
        if (dir != null && !dir.exists()) {
            dir.mkdirs();
        }
    }
}