Example usage for java.io File getParent

List of usage examples for java.io File getParent

Introduction

In this page you can find the example usage for java.io File getParent.

Prototype

public String getParent() 

Source Link

Document

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:de.viaboxx.nlstools.tasks.CopyBundlesTask.java

/**
 * @param controlFile//from   w ww  .  j a  va2s  .  c o m
 * @return key = plain name.xml, value = xml-bundle file
 * @throws IOException
 */
static Map<String, File> readControlFileMapping(File controlFile) throws IOException {
    FileReader reader = new FileReader(controlFile);
    List<String> controlFileContent = IOUtils.readLines(reader);
    reader.close();

    Map<String, File> controlFileMapping = new HashMap(controlFileContent.size());
    for (String line : controlFileContent) {
        line = line.trim();
        if (line.length() > 0) {
            int i = line.lastIndexOf('/');
            String name = line.substring(i + 1);
            controlFileMapping.put(name, new File(controlFile.getParent(), line));
        }
    }
    return controlFileMapping;
}

From source file:com.datatorrent.stram.StramMiniClusterTest.java

@BeforeClass
public static void setup() throws InterruptedException, IOException {
    LOG.info("Starting up YARN cluster");
    conf = StramClientUtils.addDTDefaultResources(conf);
    conf.setInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, 64);
    conf.setInt("yarn.nodemanager.vmem-pmem-ratio", 20); // workaround to avoid containers being killed because java allocated too much vmem
    conf.setStrings("yarn.scheduler.capacity.root.queues", "default");
    conf.setStrings("yarn.scheduler.capacity.root.default.capacity", "100");

    StringBuilder adminEnv = new StringBuilder(1024);
    if (System.getenv("JAVA_HOME") == null) {
        adminEnv.append("JAVA_HOME=").append(System.getProperty("java.home"));
        adminEnv.append(",");
    }/*from  w  w  w  . j a  v a2 s.c  o m*/
    adminEnv.append("MALLOC_ARENA_MAX=4"); // see MAPREDUCE-3068, MAPREDUCE-3065
    adminEnv.append(",");
    adminEnv.append("CLASSPATH=").append(getTestRuntimeClasspath());

    conf.set(YarnConfiguration.NM_ADMIN_USER_ENV, adminEnv.toString());

    if (yarnCluster == null) {
        yarnCluster = new MiniYARNCluster(StramMiniClusterTest.class.getName(), 1, 1, 1);
        yarnCluster.init(conf);
        yarnCluster.start();
    }

    conf = yarnCluster.getConfig();
    URL url = Thread.currentThread().getContextClassLoader().getResource("yarn-site.xml");
    if (url == null) {
        LOG.error("Could not find 'yarn-site.xml' dummy file in classpath");
        throw new RuntimeException("Could not find 'yarn-site.xml' dummy file in classpath");
    }
    File confFile = new File(url.getPath());
    yarnCluster.getConfig().set("yarn.application.classpath", confFile.getParent());
    OutputStream os = new FileOutputStream(confFile);
    LOG.debug("Conf file: {}", confFile);
    yarnCluster.getConfig().writeXml(os);
    os.close();

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        LOG.info("setup thread sleep interrupted. message=" + e.getMessage());
    }
}

From source file:com.liferay.util.FileUtil.java

public static boolean containsParentFolder(File file, File[] folders) {
    for (File folder : Arrays.asList(folders)) {

        if (file.getParent().equalsIgnoreCase(folder.getPath())) {
            return true;
        }/*from www.  j  ava  2  s . c o m*/
    }

    return false;
}

From source file:utils.APIImporter.java

/**
 * function to unzip the imported folder
 * @param zipFile zip file path//from   ww  w . ja  v a2s . c o  m
 * @param outputFolder folder to copy the zip content
 * @throws APIImportException
 */
private static void unzipFolder(String zipFile, String outputFolder) throws APIImportException {
    byte[] buffer = new byte[1024];

    try {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();
        FileOutputStream fos = null;
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();
            fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        IOUtils.closeQuietly(zis);
        IOUtils.closeQuietly(fos);
        //            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //            //get the zipped file list entry
        //            ZipEntry ze = zis.getNextEntry();
        //            FileOutputStream fos = null;
        //            while(ze!=null){
        //                String fileName = ze.getName();
        //                File newFile = new File(outputFolder + File.separator + fileName);
        //                //create all non exists folders
        //                //else it hit FileNotFoundException for compressed folder
        //                boolean directoryStatus = new File(newFile.getParent()).mkdirs();
        //                while (directoryStatus){
        //                     fos = new FileOutputStream(newFile);
        //                    int len;
        //                    while ((len = zis.read(buffer)) > 0) {
        //                        fos.write(buffer, 0, len);
        //                    }
        //                    ze = zis.getNextEntry();
        //                }
        //            }
        //            zis.closeEntry();
        //            IOUtils.closeQuietly(fos);
        //            IOUtils.closeQuietly(zis);
    } catch (IOException e) {
        String errorMsg = "Cannot extract the zip entries ";
        log.error(errorMsg, e);
        throw new APIImportException(errorMsg, e);
    }
}

From source file:com.glaf.core.util.FileUtils.java

public static boolean mkdirsWithExistsCheck(File dir) {
    if (dir.mkdir() || dir.exists()) {
        return true;
    }//from  w ww  .j  av  a 2  s .  c o  m
    File canonDir = null;
    try {
        canonDir = dir.getCanonicalFile();
    } catch (IOException e) {
        return false;
    }
    String parent = canonDir.getParent();
    return (parent != null)
            && (mkdirsWithExistsCheck(new File(parent)) && (canonDir.mkdir() || canonDir.exists()));
}

From source file:Main.java

/**
 * Change the extension of specified file.<br>
 * Usage :<br>/*from w ww .  j  a va  2 s  .  c o  m*/
 * f.renameTo(MyFileUtility.reExtension(f, "jpg")) ;<br>
 * As we know ,File class is a dummy File , <br>
 * thus , you must follow the usage to change extension.
 * @param fin File input
 * @param newExtension newExtension without '.'
 * @return File with new extension
 */
public static File reExtension(File fin, String newExtension) {
    //Usage: f.renameTo(MyFileUtility.reExtension(f, "jpg")) ;
    StringTokenizer strTokener = new StringTokenizer(fin.getName(), ".");
    //For a file may has many '.' in its file name , we use a collection to stroe it.
    ArrayList<String> strVec = new ArrayList<String>();
    while (strTokener.hasMoreTokens())
        strVec.add(strTokener.nextToken());

    String newName = "";
    //Give up the original extension
    for (int i = 0; i != strVec.size() - 1; ++i) {
        newName += strVec.get(i);
        newName += ".";
    }
    newName += newExtension;
    return new File(fin.getParent() + "\\" + newName);
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * Return the directory where temporary files are created.
 * /*from  ww w  .  j  a va2s  .  com*/
 * @return the directory where temporary files are created.
 * @throws IOException
 */
public static String getTempDirectory() throws IOException {
    String tempDirectory = System.getProperty("java.io.tmpdir"); // works in JDK 1.4+
    if (tempDirectory != null) {

        return tempDirectory;
    }
    File f = File.createTempFile("directory-finder", ".tmp"); // fall back for politeness
    f.delete();
    return f.getParent();
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void writeObject(Object o, File file) {
    File tmpFile = new File(file.getParent(), file.getName() + ".tmp");

    try {// ww  w  . ja v a2 s.  c  o m
        final FileOutputStream fous = new FileOutputStream(tmpFile);
        try {
            ObjectOutput output = new ObjectOutputStream(fous);
            output.writeObject(o);
        } finally {
            Utils.closeQuietly(fous);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!tmpFile.renameTo(file)) {
        throw new RuntimeException(
                format("Could not rename [%s] to [%s]", tmpFile.getAbsolutePath(), file.getAbsolutePath()));
    }
}

From source file:de.erdesignerng.util.JasperUtils.java

public static JasperPrint runJasperReport(File aModelXMLFile, File aJRXMLFile)
        throws JRException, FileNotFoundException {

    String theFileName = aJRXMLFile.getAbsolutePath();
    int p = theFileName.indexOf(".jrxml");
    String theTemplateName = theFileName.substring(0, p) + ".jasper";

    File theTemplateFile = new File(theTemplateName);

    JasperDesign theDesign = JRXmlLoader.load(new FileInputStream(aJRXMLFile));
    JRQuery theQuery = theDesign.getQuery();
    String theQueryText = null;/*from ww  w  . ja v a  2  s.  co  m*/

    if (theQuery != null) {
        theQueryText = theQuery.getText();
    }
    if (StringUtils.isEmpty(theQueryText)) {
        throw new RuntimeException("Cannot extract query from Jasper template");
    }

    Map<String, Object> theParams = new HashMap<>();
    theParams.put(JRParameter.REPORT_LOCALE, Locale.getDefault());

    String theSubreportDir = theTemplateFile.getParent();
    if (!theSubreportDir.endsWith(File.separator)) {
        theSubreportDir += File.separator;
    }
    theParams.put("SUBREPORT_DIR", theSubreportDir);

    JRXmlDataSource theDataSource = new JRXmlDataSource(aModelXMLFile, theQueryText);
    return JasperFillManager.fillReport(new FileInputStream(theTemplateFile), theParams, theDataSource);
}

From source file:io.github.bonigarcia.wdm.Downloader.java

public static final File extractMsi(File msi) throws IOException {
    File tmpMsi = new File(Files.createTempDir().getAbsoluteFile() + File.separator + msi.getName());
    Files.move(msi, tmpMsi);/* w w  w  . java  2 s  .c o  m*/
    log.trace("Temporal msi file: {}", tmpMsi);

    Process process = Runtime.getRuntime()
            .exec(new String[] { "msiexec", "/a", tmpMsi.toString(), "/qb", "TARGETDIR=" + msi.getParent() });
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        log.error("Exception waiting to msiexec to be finished", e);
    } finally {
        process.destroy();
    }

    tmpMsi.delete();

    Collection<File> listFiles = FileUtils.listFiles(new File(msi.getParent()), new String[] { "exe" }, true);
    return listFiles.iterator().next();
}