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:ar.com.init.agros.reporting.dj.ReportExporter.java

/**
 * The path to the file must exist./*from  w w w. j  a va 2  s . c om*/
 * @param jp
 * @param path
 * @throws JRException
 * @throws FileNotFoundException
 */
public static void exportReportPDF(JasperPrint jp, String path) throws JRException, FileNotFoundException {
    JRPdfExporter exporter = new JRPdfExporter();

    File outputFile = new File(path);
    File parentFile = outputFile.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(outputFile);

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);

    exporter.exportReport();

    logger.info("Report exported: " + path);
}

From source file:io.servicecomb.foundation.common.utils.Log4jUtils.java

private static void outputFile(List<Resource> resList, Properties properties)
        throws IOException, URISyntaxException {
    //??class???outputFile??log???
    //must create org.slf4j.impl.Log4jLoggerAdapter by LoggerExtFactory
    //in order to redefine Log4jLoggerAdapter before other class load Log4jLoggerAdapter
    Logger log = LoggerFactory.getLogger(Log4jUtils.class);

    String content = genFileContext(resList, properties);
    //????,??/*w  w  w . j  av  a  2s .  co  m*/
    //log.info("Merged log4j:\n{}", content);

    Resource res = resList.get(resList.size() - 1);
    // ?res.getFilejar??getFile
    File file = new File(res.getURL().getPath());
    if (!file.getParentFile().canWrite()) {
        log.error("Can not output {},because can not write to directory of file {}", MERGED_FILE,
                res.getURL().getPath());
        return;
    }

    File mergedfile = new File(res.getFile().getParentFile(), MERGED_FILE);
    FileUtils.writeStringToFile(mergedfile, content);
    log.info("Write merged log4j config file to {}", mergedfile.getAbsolutePath());
}

From source file:Utils.java

/**
 * Unpack a zip file//from  w  ww  . ja v a2s . c  om
 * 
 * @param theFile
 * @param targetDir
 * @return the file
 * @throws IOException
 */
public static File unpackArchive(File theFile, File targetDir) throws IOException {
    if (!theFile.exists()) {
        throw new IOException(theFile.getAbsolutePath() + " does not exist");
    }
    if (!buildDirectory(targetDir)) {
        throw new IOException("Could not create directory: " + targetDir);
    }
    ZipFile zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File file = new File(targetDir, File.separator + entry.getName());
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }
    zipFile.close();
    return theFile;
}

From source file:io.github.bunnyblue.droidfix.classcomputer.ClassComputer.java

public static void processDiffClassToApk(String patchClassDir) {
    File patchClassesDir = new File(patchClassDir);
    File patchjar = new File(
            patchClassesDir.getParentFile().getAbsolutePath() + File.separator + "classes.jar");
    File patchDex = new File(patchClassesDir.getParentFile().getAbsolutePath() + File.separator + "patch.apk");
    ZipUtil.pack(patchClassesDir, patchjar);
    String dxArgs[] = new String[] { "--dex", "--output=" + patchDex.getAbsolutePath(),
            patchjar.getAbsolutePath() };
    Main.main(dxArgs);/*from ww  w.ja  va2 s .  com*/
    //        File patchPkg=new File(patchClassesDir.getAbsolutePath()+File.separator+"patch.apk");
    //      //  ZipEntrySource []zipEntrySources=new ZipEntrySource[1];
    //        ZipUtil.packEntry(patchDex,patchPkg);

}

From source file:it.cnr.icar.eric.client.xml.registry.util.KeystoreUtil.java

/**
 * Create keystore directory if it does not already exist
 *
 * @param keystoreFile Path to keystore file
 * @throws JAXRException Thrown if directory could not be created
 *//*from  ww  w. j a v  a  2  s  .  co m*/
public static void createKeystoreDirectory(File keystoreFile) throws JAXRException {
    File keystoreDir = keystoreFile.getParentFile();

    try {
        // Ignore return value of mkdirs, returns false if directories
        // already exist
        keystoreDir.mkdirs();
    } catch (SecurityException e) {
        log.error(e);
        throw new JAXRException(JAXRResourceBundle.getInstance().getString("message.error.not.create.directory",
                new Object[] { keystoreDir.getAbsolutePath() }));
    }
}

From source file:com.incapture.rapgen.output.OutputWriter.java

/**
 * Some files are composed of multiple templates. So the map passed in here is filename to template order to template.
 * E.g. "file.txt"->1->"some code" "file.txt"->2->"other code" and so on.
 *
 * @param rootFolder/*from  w ww  .  j  a v a  2  s .c om*/
 * @param pathToTemplate
 */
public static void writeMultiPartTemplates(String rootFolder,
        Map<String, Map<String, StringTemplate>> pathToTemplate) {
    // For each file, dump the templates
    for (Map.Entry<String, Map<String, StringTemplate>> entry : pathToTemplate.entrySet()) {
        File file = new File(rootFolder, entry.getKey());
        file.getParentFile().mkdirs();

        BufferedWriter bow = null;
        try {
            bow = new BufferedWriter(new FileWriter(file));
            Set<String> sections = entry.getValue().keySet();
            SortedSet<String> sorted = new TreeSet<String>();
            sorted.addAll(sections);
            for (String sec : sorted) {
                bow.write(entry.getValue().get(sec).toString());
                bow.newLine();
            }
            bow.close();
        } catch (IOException e) {
            System.err.println(e.getMessage());
        } finally {
            if (bow != null) {
                try {
                    bow.close();
                } catch (IOException e) {
                    System.err.println("Error closing output stream: " + ExceptionToString.format(e));
                }
            }
        }
    }
}

From source file:eu.usrv.amdiforge.core.GraveFileHandler.java

public static File getSaveFolder(World world) {
    File dummy = world.getSaveHandler().getMapFileFromName("dummy");
    return dummy.getParentFile();
}

From source file:Main.java

/**
 * Compute the root directory of this maven project. This will result in the
 * same directory no matter if executed from Eclipse, this maven project root or
 * any parent maven pom directory. /*from  ww  w.  ja  v a 2  s  .c o m*/
 * 
 * @param anyTestClass Any test class *local* to the maven project, i.e that 
 * only exist in this maven project.
 * 
 * @param child The file that should be 
 * @return The root directory of this maven project.
 */
public static File computeMavenProjectRoot(Class<?> anyTestClass) {
    final String clsUri = anyTestClass.getName().replace('.', '/') + ".class";
    final URL url = anyTestClass.getClassLoader().getResource(clsUri);
    final String clsPath = url.getPath();
    // located in ./target/test-classes or ./eclipse-out/target
    final File target_test_classes = new File(clsPath.substring(0, clsPath.length() - clsUri.length()));
    // get parent's parent
    return target_test_classes.getParentFile().getParentFile();
}

From source file:ar.com.init.agros.reporting.dj.ReportExporter.java

public static void exportReportPlainXLS(JasperPrint jp, String path) throws JRException, FileNotFoundException {
    //      JRXlsExporter exporter = new JRXlsExporter();
    JExcelApiExporter exporter = new JExcelApiExporter();

    File outputFile = new File(path);
    File parentFile = outputFile.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();// w  ww  . j a v a2 s.  c  om
    }
    FileOutputStream fos = new FileOutputStream(outputFile);

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);

    exporter.exportReport();

    logger.info("Report exported: " + path);

}

From source file:Main.java

public static String getRelativePath(String basePath, String pathToRelativize) throws IOException {
    File baseFile = new File(basePath);
    if (baseFile.isFile()) {
        baseFile = baseFile.getParentFile();
    }/*from w  w w . jav  a2  s.  c o  m*/

    return getRelativeFileInternal(baseFile.getCanonicalFile(), new File(pathToRelativize).getCanonicalFile());
}