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:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.FileCopier.java

/**
 * Copy the given source file or directory to the given destination, keeping the same creation date as the source.
 *
 * @param sourceFileOrDirectory      the source file or directory
 * @param destinationFileOrDirectory the destination file or directory
 * @throws IOException if an IO error occurs during the copy
 *///www  .j  a v a 2 s.co m
public static void copyFileOrDirectory(final File sourceFileOrDirectory, final File destinationFileOrDirectory)
        throws IOException {

    final File parentDirectory = destinationFileOrDirectory.getParentFile();
    if (!parentDirectory.exists()) {
        boolean success = parentDirectory.mkdirs();

        if (!success) {
            throw new IOException("Directory '" + parentDirectory + "' was not successfully created.");
        }
    }

    if (sourceFileOrDirectory.isDirectory()) {
        FileUtils.copyDirectory(sourceFileOrDirectory, destinationFileOrDirectory);
    } else {
        FileUtils.copyFile(sourceFileOrDirectory, destinationFileOrDirectory);
    }
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

public static void unzipFile(File destFolder, File zipFile) throws IOException {
    ZipInputStream zipStream = null;
    try {/*from  ww w.  j a va  2s  .  co m*/
        if (!destFolder.exists()) {
            destFolder.mkdirs();
        }

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile));
        zipStream = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zipStream.getNextEntry()) != null) {
            // get output file
            String name = entry.getName();
            if (name.startsWith("/") || name.startsWith("\\"))
                name = name.substring(1);
            File file = new File(destFolder, name);
            // ensure directory exists
            File dir = file.getParentFile();
            if (!dir.exists())
                dir.mkdirs();
            IOUtils.copy(zipStream, new FileOutputStream(file));
        }
    } finally {
        if (zipStream != null)
            zipStream.close();
    }
}

From source file:com._17od.upm.util.Preferences.java

public static void load() throws FileNotFoundException, IOException {

    //Check for the system property PREF_FILE_SYS_PROP. If supplied it will give the name
    //of the properties file to use. If it's not given then use the properties file in the
    //user's home directory (which may or may not exist)

    // Mac and Linux have different places to store configuration files -
    // set PREF_FILE_SYS_PROP appropriately.
    if (!System.getProperties().containsKey(PREF_FILE_SYS_PROP)) {
        if (PlatformSpecificCode.isLinux()) {
            String configBase = System.getenv("XDG_CONFIG_HOME");
            if (null == configBase || configBase.trim().equals("")) {
                configBase = System.getProperty("user.home") + System.getProperty("file.separator") + ".config";
            }//from   w w w  . j av  a2  s .  c o m
            System.setProperty(PREF_FILE_SYS_PROP,
                    configBase + System.getProperty("file.separator") + "upm.properties");
        } else if (PlatformSpecificCode.isMAC()) {
            System.setProperty(PREF_FILE_SYS_PROP,
                    System.getProperty("user.home") + System.getProperty("file.separator") + "Library"
                            + System.getProperty("file.separator") + "Preferences"
                            + System.getProperty("file.separator") + "upm.properties");
        }
    }

    propertiesFile = System.getProperty(PREF_FILE_SYS_PROP);
    if (propertiesFile == null || propertiesFile.trim().equals("")) {
        propertiesFile = PREF_FILE;
    }

    // Create propertiesFile directories if it doesn't exist
    File prefs = new File(propertiesFile);
    prefs.getParentFile().mkdirs();

    //Attempt to load the properties
    try {
        if (log.isInfoEnabled()) {
            log.info("Loading the properties file [" + propertiesFile + "]");
        }
        preferences = new Properties();
        preferences.load(new FileInputStream(propertiesFile));
    } catch (FileNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug("Property file not found. Will be created the next time the properties are saved.");
        }
    }
}

From source file:com.admc.jcreole.CreoleParseTest.java

@Parameters
public static List<Object[]> creoleFiles() throws IOException {
    if (!pCreoleInRoot.isDirectory())
        throw new IllegalStateException("Dir missing: " + pCreoleInRoot.getAbsolutePath());
    if (!nCreoleInRoot.isDirectory())
        throw new IllegalStateException("Dir missing: " + nCreoleInRoot.getAbsolutePath());
    if (pWorkOutRoot.exists())
        FileUtils.deleteDirectory(pWorkOutRoot);
    pWorkOutRoot.mkdir();/*from  w w w  .j  ava  2 s. c  o m*/
    if (nWorkOutRoot.exists())
        FileUtils.deleteDirectory(nWorkOutRoot);
    nWorkOutRoot.mkdir();
    List<Object[]> params = new ArrayList<Object[]>();
    File eFile;
    for (File f : FileUtils.listFiles(pCreoleInRoot, new String[] { "creole" }, true)) {
        eFile = new File(f.getParentFile(), f.getName().replaceFirst("\\..*", "") + ".html");
        params.add(new Object[] { f, eFile,
                (eFile.isFile()
                        ? new File(pWorkOutRoot,
                                f.getParentFile().equals(pCreoleInRoot) ? eFile.getName()
                                        : (f.getParent().substring(pCreoleInRootPath.length()) + FSEP
                                                + eFile.getName()))
                        : null),
                Boolean.TRUE });
    }
    String name;
    for (File f : FileUtils.listFiles(nCreoleInRoot, new String[] { "creole" }, true)) {
        name = f.getName().replaceFirst("\\..*", "") + ".html";
        params.add(new Object[] { f, null,
                new File(nWorkOutRoot,
                        f.getParentFile().equals(nCreoleInRoot) ? name
                                : (f.getParent().substring(nCreoleInRootPath.length()) + FSEP + name)),
                Boolean.FALSE });
    }
    return params;
}

From source file:com.ansorgit.plugins.bash.BashTestUtils.java

private static String computeBasePath() {
    String configuredDir = StringUtils.stripToNull(System.getenv("BASHSUPPORT_TESTDATA"));
    if (configuredDir != null) {
        File dir = new File(configuredDir);
        if (dir.isDirectory() && dir.exists()) {
            return dir.getAbsolutePath();
        }// ww w  .j a va  2  s  .  c o m
    }

    //try to find out from the current classloader
    URL url = BashTestUtils.class.getClassLoader().getResource("log4j.xml");
    if (url != null) {
        try {
            File resourceFile = new File(url.toURI());
            //we need to cut the out dir and the other resource paths
            File basePath = resourceFile.getParentFile().getParentFile().getParentFile().getParentFile();
            if (basePath != null && basePath.isDirectory()) {
                return basePath + File.separator + "testData";
            }
        } catch (Exception e) {
            //ignore, use fallback below
        }
    }

    return null;
}

From source file:com.geewhiz.pacify.utils.FileUtils.java

public static File createEmptyFileWithSamePermissions(File forFile, String filePrefix) {
    try {// www  .ja  v a2  s.co m
        File folder = forFile.getParentFile();

        File tmp = File.createTempFile(filePrefix, ".tmp", folder);
        setPosixPermissions(getPosixPermissions(forFile), tmp);
        return tmp;

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.photon.maven.plugins.android.common.JarHelper.java

/** Unjars the specified jar file into the the specified directory
 *
 * @param jarFile// w w w.j  a  va2 s.  co m
 * @param outputDirectory
 * @param unjarListener
 * @throws IOException
 */
public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener)
        throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (unjarListener.include(entry)) {
            if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) {
                throw new IOException("Error creating output directory: " + entryFile.getParentFile());
            }

            // If the entry is an actual file, unzip that too
            if (!entry.isDirectory()) {
                final InputStream in = jarFile.getInputStream(entry);
                try {
                    final OutputStream out = new FileOutputStream(entryFile);
                    try {
                        IOUtil.copy(in, out);
                    } finally {
                        IOUtils.closeQuietly(out);
                    }
                } finally {
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
}

From source file:au.org.ala.delta.util.FileUtils.java

private static File parent(File start, File parent) {
    if (start.equals(parent) || start.getParentFile() == null) {
        return start;
    } else {/*ww  w .j  a  v a  2 s. co  m*/
        return parent(start.getParentFile(), parent);
    }
}

From source file:com.izforge.izpack.util.file.FileUtils.java

/**
 * This was originally an emulation of {@link File#getParentFile} for JDK 1.1,
 * but it is now implemented using that method (Ant 1.6.3 onwards).
 *
 * @param f the file whose parent is required.
 * @return the given file's parent, or null if the file does not have a
 *         parent./*from  ww w .j  ava2  s  .c  o m*/
 */
public static File getParentFile(File f) {
    return (f == null) ? null : f.getParentFile();
}

From source file:Main.java

public static Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;//from  w  ww .  jav  a2s .com
    if (drawable instanceof BitmapDrawable) {
        bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
        return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}