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

private static boolean handleFile(int mode, byte[] key, byte[] iv, String sourceFilePath, String destFilePath) {

    File sourceFile = new File(sourceFilePath);
    File destFile = new File(destFilePath);

    try {/*from w ww  .j av  a  2  s  .co  m*/
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!destFile.getParentFile().exists())
                destFile.getParentFile().mkdirs();
            destFile.createNewFile();

            InputStream in = new FileInputStream(sourceFile);
            OutputStream out = new FileOutputStream(destFile);
            Cipher cipher = initCipher(mode, key, iv, AES_CFB_NOPADDING);
            CipherInputStream cin = new CipherInputStream(in, cipher);

            byte[] b = new byte[1024];
            int read = 0;
            while ((read = cin.read(b)) != -1) {
                out.write(b, 0, read);
                out.flush();
            }

            cin.close();
            in.close();
            out.close();

            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static String imageToLocal(Bitmap bitmap, String name) {
    String path = null;/*  w ww  .  j av a  2 s .c o  m*/
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

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

/**
 * Checks whether a given file is a symbolic link.
 * <p/>//  w  w  w. j ava  2s.  com
 * <p>It doesn't really test for symbolic links but whether the
 * canonical and absolute paths of the file are identical--this
 * may lead to false positives on some platforms.</p>
 *
 * @param parent the parent directory of the file to test
 * @param name   the name of the file to test.
 * @return true if the file is a symbolic link.
 * @throws IOException on error.
 */
public static boolean isSymbolicLink(File parent, String name) throws IOException {
    if (parent == null) {
        File f = new File(name);
        parent = f.getParentFile();
        name = f.getName();
    }
    File toTest = new File(parent.getCanonicalPath(), name);
    return org.apache.commons.io.FileUtils.isSymlink(toTest);
}

From source file:net.rwx.maven.asciidoc.utils.FileUtils.java

public static String uncompress(InputStream is, String destination) throws IOException {

    BufferedInputStream in = new BufferedInputStream(is);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzIn);

    TarArchiveEntry entry = tarInput.getNextTarEntry();
    do {//w ww .j a  va2s.  c om
        File f = new File(destination + "/" + entry.getName());
        FileUtils.forceMkdir(f.getParentFile());

        if (!f.isDirectory()) {
            OutputStream os = new FileOutputStream(f);
            byte[] content = new byte[(int) entry.getSize()];
            int byteRead = 0;
            while (byteRead < entry.getSize()) {
                byteRead += tarInput.read(content, byteRead, content.length - byteRead);
                os.write(content, 0, byteRead);
            }

            os.close();
            forceDeleteOnExit(f);
        }
        entry = tarInput.getNextTarEntry();
    } while (entry != null);

    gzIn.close();

    return destination;
}

From source file:com.compomics.pladipus.core.control.util.ZipUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;// w  w w  .  ja  va  2s  .c  o  m
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    LOGGER.debug("Extracting: " + entry);

    try (BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
    }
}

From source file:de.tudarmstadt.ukp.similarity.experiments.coling2012.util.Features2Arff.java

@SuppressWarnings("unchecked")
private static String toArffString(Dataset dataset, Collection<File> csvFiles) throws IOException {
    // Create the Arff header
    StringBuilder arff = new StringBuilder();
    arff.append("@relation temp-relation" + LF);
    arff.append(LF);/*from ww  w . ja  v  a 2 s  .com*/

    // Init data object
    Map<Integer, List<String>> data = new HashMap<Integer, List<String>>();

    for (File file : csvFiles) {
        String feature = file.getParentFile().getName() + "/"
                + file.getName().substring(0, file.getName().length() - 4);
        // feature = feature.replaceAll(",", "");

        // Add the attribute to the Arff header
        arff.append("@attribute " + feature + " numeric" + LF);

        // Read data
        List<String> lines = FileUtils.readLines(file);
        for (int doc = 1; doc <= lines.size(); doc++) {
            String line = lines.get(doc - 1);

            if (line.length() > 0) // Ignore empty lines
            {
                Double value = Double.parseDouble(line); // There's just the score on the line, nothing else.

                // Get doc object in data list
                List<String> docObj;
                if (data.containsKey(doc))
                    docObj = data.get(doc);
                else
                    docObj = new ArrayList<String>();

                // Put data
                docObj.add(value.toString());
                data.put(doc, docObj);
            }
        }
    }

    // Read gold standard
    List<String> lines = ColingUtils.readGoldstandard(dataset);

    // Get a list of all classes (as they differ from dataset to dataset
    Set<String> allClasses = new HashSet<String>();
    for (String line : lines) {
        allClasses.add(line);
    }

    // Add gold attribute to attribute list in header
    arff.append("@attribute gold { " + StringUtils.join(allClasses, ", ") + " }" + LF);

    // Add gold similarity score 
    for (int doc = 1; doc <= lines.size(); doc++) {
        String value = lines.get(doc - 1);

        List<String> docObj = data.get(doc);
        docObj.add(value);
        data.put(doc, docObj);
    }

    // Finalize header
    arff.append(LF);
    arff.append("@data" + LF);

    // Write data
    for (int i = 1; i <= data.keySet().size(); i++) {
        String dataItem = StringUtils.join(data.get(i), ",");

        arff.append(dataItem + LF);
    }

    return arff.toString();
}

From source file:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java

/**
 * Extracts the form file from a resource file (ex shapefile).
 * //ww  w  . j a  va  2  s . c o  m
 * @param resourceFile the resource that has a form sidecar file.
 * @return the form file or null, if it doesn't exist.
 */
public static File getFormFile(File resourceFile) {

    String baseName = FilenameUtils.getBaseName(resourceFile.getName());
    File formFile = new File(resourceFile.getParentFile(), baseName + FORM);

    if (formFile.exists()) {
        return formFile;
    } else {
        return null;
    }
}

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static void unzip(File zipFile, File outputFolder) {
    InputStream in = null;/*from   ww w  .ja  v a 2s .  c o  m*/
    OutputStream out = null;
    try {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        if (!outputFolder.isDirectory())
            outputFolder.mkdirs();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryTarget = new File(outputFolder, entry.getName());
            entryTarget.getParentFile().mkdirs();
            if (entry.isDirectory())
                entryTarget.mkdirs();
            else {
                in = zip.getInputStream(entry);
                out = new FileOutputStream(entryTarget);
                IOUtils.copy(in, out);
            }
        }
    } catch (Exception e) {
        LogHelper.error("Failed extracting " + zipFile.getName(), e);
    } finally {
        if (in != null)
            IOUtils.closeQuietly(in);
        if (out != null)
            IOUtils.closeQuietly(out);
    }
}

From source file:Main.java

/**
 * Returns the relative path beginning after the getAppFolder(appName) directory.
 * The relative path does not start or end with a '/'
 *
 * @param appName/*from w ww  .j  av  a  2s. com*/
 * @param fileUnderAppName
 * @return
 */
public static String asRelativePath(String appName, File fileUnderAppName) {
    // convert fileUnderAppName to a relative path such that if
    // we just append it to the AppFolder, we have a full path.
    File parentDir = new File(getAppFolder(appName));

    ArrayList<String> pathElements = new ArrayList<String>();

    File f = fileUnderAppName;
    while (f != null && !f.equals(parentDir)) {
        pathElements.add(f.getName());
        f = f.getParentFile();
    }

    if (f == null) {
        throw new IllegalArgumentException("file is not located under this appName (" + appName + ")!");
    }

    StringBuilder b = new StringBuilder();
    for (int i = pathElements.size() - 1; i >= 0; --i) {
        String element = pathElements.get(i);
        b.append(element);
        if (i != 0) {
            b.append(File.separator);
        }
    }
    return b.toString();

}

From source file:Main.java

private static ArrayList<String> getPathComponents(File file) {
    ArrayList<String> path = new ArrayList<String>();

    while (file != null) {
        File parentFile = file.getParentFile();

        if (parentFile == null) {
            path.add(0, file.getPath());
        } else {/*from w ww  . j  av a2 s .  co m*/
            path.add(0, file.getName());
        }

        file = parentFile;
    }

    return path;
}