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.carrotgarden.eclipse.fileinstall.util.ProjectUtil.java

/**
 * Extract class path resource to the file system.
 *///from w ww  .j av  a 2s  . co  m
public static void copyFromClasspathIntoProject(
        //
        final Class<?> klaz, //
        final String sourcePath, //
        final IProject project, //
        final String targetPath //
) throws Exception {

    final InputStream input = klaz.getResourceAsStream(sourcePath);

    final File target = file(project, targetPath);

    if (!target.exists()) {
        target.getParentFile().mkdirs();
    }

    final OutputStream output = new FileOutputStream(target);

    IOUtils.copy(input, output);

    input.close();
    output.close();

}

From source file:com.ikon.module.db.stuff.FsDataStore.java

/**
 * Write to data store /*  w  w  w .  java  2 s. co m*/
 */
public static File save(String uuid, InputStream is) throws IOException {
    log.debug("save({}, {})", uuid, is);
    File fs = resolveFile(uuid);
    fs.getParentFile().mkdirs();
    FileOutputStream fos = new FileOutputStream(fs);
    IOUtils.copy(is, fos);
    IOUtils.closeQuietly(fos);
    return fs;
}

From source file:net.fckeditor.tool.UtilsFile.java

/**
 * Iterates over a base name and returns the first non-existent file.<br />
 * This method extracts a file's base name, iterates over it until the first
 * non-existent appearance with <code>basename(n).ext</code>. Where n is a
 * positive integer starting from one./*from w  w w  .j av  a  2  s  . c  o  m*/
 * 
 * @param file
 *            base file
 * @return first non-existent file
 */
public static File getUniqueFile(final File file) {
    if (!file.exists())
        return file;

    File tmpFile = new File(file.getAbsolutePath());
    File parentDir = tmpFile.getParentFile();
    int count = 1;
    String extension = FilenameUtils.getExtension(tmpFile.getName());
    String baseName = FilenameUtils.getBaseName(tmpFile.getName());
    do {
        tmpFile = new File(parentDir, baseName + "(" + count++ + ")." + extension);
    } while (tmpFile.exists());
    return tmpFile;
}

From source file:com.skcraft.launcher.persistence.Persistence.java

/**
 * Write an object to file.//from ww w . j a va 2  s .c o  m
 *
 * @param file the file
 * @param object the object
 * @throws java.io.IOException on I/O error
 */
public static void write(File file, Object object) throws IOException {
    file.getParentFile().mkdirs();
    mapper.writeValue(file, object);
}

From source file:Main.java

private static HashMap<String, Object> getPossibleOriginalXlzSourceFile(File file) {
    HashMap<String, Object> result = new HashMap<String, Object>();
    File wantedFile = file;//from w w w  . jav a2 s  .  co m
    Boolean isFromXlzFile = false;

    try {
        File parentFile = file.getParentFile();
        File grandFile = parentFile.getParentFile();
        File f = new File(grandFile, parentFile.getName() + ".xlz");
        if (f.exists() && f.isFile()) {
            wantedFile = f;
            isFromXlzFile = true;
        }
    } catch (Exception e) {

    }

    result.put("sourceFile", wantedFile);
    result.put("isFromXlzFile", isFromXlzFile);
    return result;
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * Download from given URL to given file location.
 * @param url String//from   w ww  .j av a  2s .  co  m
 * @param filepath String
 * @return
 */
public static String download(String url, String filepath) {
    String status = "";
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpget);

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        if (filepath == null) {
            filepath = getFilePath(response); //NOSONAR
        }

        File file = new File(filepath);
        file.getParentFile().mkdirs();
        FileOutputStream fileout = new FileOutputStream(file);

        byte[] buffer = new byte[CACHE];
        int ch;
        while ((ch = is.read(buffer)) != -1) {
            fileout.write(buffer, 0, ch);
        }
        is.close();
        fileout.flush();
        fileout.close();
        status = Constant.DOWNLOADCSAR_SUCCESS;

    } catch (Exception e) {
        status = Constant.DOWNLOADCSAR_FAIL;
        LOG.error("Download csar file failed! " + e.getMessage(), e);
    }
    return status;
}

From source file:com.freedomotic.util.Unzip.java

/**
 *
 * @param zipFile//from  w w  w .  java  2 s . co m
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(String zipFile) throws IOException {

    if (StringUtils.isEmpty(zipFile)) {
        LOG.error("File path not provided, no unzipping performed");
        return;
    }

    File file = new File(zipFile);

    if (!file.exists()) {
        LOG.error("File not existing, no unzipping performed");
        return;
    }

    try (ZipFile zip = new ZipFile(file);) {
        String newPath = zipFile.substring(0, zipFile.length() - 4);
        //simulates the unzip here feature
        newPath = newPath.substring(0, newPath.lastIndexOf(File.separator));

        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                try (BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
                        FileOutputStream fos = new FileOutputStream(destFile);
                        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);) {

                    int currentByte;

                    // establish buffer for writing file
                    byte[] data = new byte[BUFFER];

                    // read and write until last byte is encountered
                    while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                } catch (IOException ex) {
                    LOG.error(Freedomotic.getStackTraceInfo(ex));
                }
            }

            if (currentEntry.endsWith(".zip")) {
                // found a zip file, try to open
                unzip(destFile.getAbsolutePath());
            }
        }
    }
}

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

public static String getRelativePath(File ancestor, File file, String separator) {
    String path = file.getName();
    File parent = file.getParentFile();
    while (parent != null && !parent.equals(ancestor)) {
        path = parent.getName() + separator + path;
        parent = parent.getParentFile();
    }// ww  w.  java  2  s  . c o  m
    return path;
}

From source file:Main.java

/**
 * Look for the resource-directory in the current directory or the directories above. Then look for the
 * raw-directory underneath the resource-directory.
 *///from  w  ww.  j  a  va  2 s  .  c o  m
protected static File findRawDir(File dir) {
    for (int i = 0; dir != null && i < 20; i++) {
        File rawDir = findResRawDir(dir);
        if (rawDir != null) {
            return rawDir;
        }
        dir = dir.getParentFile();
    }
    return null;
}

From source file:com.meli.client.controller.AppController.java

private static void processQueryFile(String fileName, String countryCode) {
        System.out.println("processQueryFile entered..");

        File file = new File(fileName);
        if (!file.isDirectory())
            file = file.getParentFile();

        if (file != null && file.exists()) {
            String line;/*from   w w w  . j a v  a  2s.c  o m*/
            try (InputStream fis = new FileInputStream(file);
                    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
                    BufferedReader br = new BufferedReader(isr);) {
                while ((line = br.readLine()) != null) {
                    doApiCalls(line, countryCode);
                }
            } catch (IOException ex) {
                Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            System.out.println("wasnt a valid file");
        }
    }