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.floreantpos.report.ReportUtil.java

private static JasperReport compileReport(String reportName) throws Exception {
    InputStream in = null;//  w ww . j a v a 2s  .  c o m
    InputStream in2 = null;
    FileOutputStream out = null;
    File jasperFile = null;

    try {
        File jrxmlFile = new File(
                ReceiptPrintService.class.getResource(USER_REPORT_DIR + reportName + ".jrxml").getFile()); //$NON-NLS-1$
        File dir = jrxmlFile.getParentFile();
        jasperFile = new File(dir, reportName + ".jasper"); //$NON-NLS-1$

        in = ReceiptPrintService.class.getResourceAsStream(USER_REPORT_DIR + reportName + ".jrxml"); //$NON-NLS-1$
        out = new FileOutputStream(jasperFile);
        JasperCompileManager.compileReportToStream(in, out);

        in2 = ReceiptPrintService.class.getResourceAsStream(USER_REPORT_DIR + reportName + ".jasper"); //$NON-NLS-1$
        return (JasperReport) JRLoader.loadObject(in2);

    } catch (Exception e) {
        IOUtils.closeQuietly(out);
        if (jasperFile != null) {
            jasperFile.delete();
        }

        throw e;
    }

    finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(in2);
        IOUtils.closeQuietly(out);
    }
}

From source file:ijfx.ui.utils.NamingUtils.java

public static File replaceWithExtension(File originalFile, String extension) {
    String basename = FilenameUtils.getBaseName(originalFile.getName());
    if (extension.startsWith(".") == false)
        extension = "." + extension;
    return new File(originalFile.getParentFile(), basename + extension);
}

From source file:com.javacreed.apps.mail.Model.java

public static File getParent(final File file) {
    if (file == null) {
        return null;
    }//from  ww  w .  j  a  va 2 s . c  o  m

    return file.getParentFile();
}

From source file:com.yahoo.bard.webservice.util.Utils.java

/**
 * Create parent directories if they don't exist in a given file path.
 *
 * @param path  The pathname/*from  ww  w  . j  a va2  s .com*/
 */
public static void createParentDirectories(String path) {
    File targetFile = new File(path);
    File parent = targetFile.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent);
    }
}

From source file:org.calrissian.restdoclet.writer.swagger.SwaggerWriter.java

private static void writeApi(String resource, Collection<Endpoint> endpoints, Configuration config)
        throws IOException {
    Map<String, Collection<Endpoint>> pathGroups = groupPaths(endpoints);

    File apiFile = new File("./" + API_DOC_DIR, resource);
    apiFile.getParentFile().mkdirs();

    Collection<Api> apis = new ArrayList<Api>(pathGroups.size());
    for (Entry<String, Collection<Endpoint>> entry : pathGroups.entrySet())
        apis.add(new Api(entry.getKey(), "", getOperations(entry.getValue())));

    mapper.writerWithDefaultPrettyPrinter().writeValue(new FileOutputStream(apiFile),
            new ApiListing(SWAGGER_VERSION, config.getUrl(), resource, config.getApiVersion(), apis));
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Creates the file if it doesn't exist yet
 *
 * @param file The file to create//  w ww  .ja v a 2 s .  c  o  m
 * @return The same file that was passed
 * @throws IOException When failed to write to a file
 */
public static File createFile(File file) throws IOException {
    if (!file.exists()) {
        if (file.getParentFile() != null)
            file.getParentFile().mkdirs();
        file.createNewFile();
    }

    return file;
}

From source file:org.dkpro.similarity.experiments.sts2013.util.Features2Arff.java

@SuppressWarnings("unchecked")
private static String toArffString(Collection<File> csvFiles, File goldFile) throws IOException {
    // Create the Arff header
    StringBuilder arff = new StringBuilder();
    arff.append("@relation temp-relation" + LF);
    arff.append(LF);//from  w  w  w  .  j a  va2  s .  co  m

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

    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.

                // Limit to [0;5] interval
                if (value > 5.0)
                    value = 5.0;
                if (value < 0.0)
                    value = 0.0;

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

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

    // Add gold attribute to attribute list in header
    // We also need to do this for unlabeled data
    arff.append("@attribute gold real" + LF);

    // Add gold similarity score 
    List<String> lines;
    if (goldFile != null) {
        lines = FileUtils.readLines(goldFile);
    } else {
        lines = new ArrayList<String>();
        for (int i = 0; i < FileUtils.readLines(csvFiles.iterator().next()).size(); i++)
            lines.add("0.0");
    }

    for (int doc = 1; doc <= lines.size(); doc++) {
        System.out.println(lines.get(doc - 1).length());
        System.out.println(lines.get(doc - 1));
        if (lines.get(doc - 1).length() == 0) {
            System.out.println("here2");
            break;
        }

        double value = Double.parseDouble(lines.get(doc - 1));

        System.out.println(doc);

        List<Double> 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:com.aliyun.odps.local.common.utils.ArchiveUtils.java

@SuppressWarnings("rawtypes")
public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {/*from w ww .  j a v a 2s . c  o m*/
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Uncompress *.zip files./*from   www  . jav  a 2  s . c o  m*/
 * @param fileName
 *        : file to be uncompress
 */
@SuppressWarnings("unchecked")
public static void decompress(String fileName) {
    File sourceFile = new File(fileName);
    String filePath = sourceFile.getParent() + "/";
    try {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration en = zipFile.entries();
        byte[] data = new byte[BUFFER];
        while (en.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bis.close();
            bos.close();
        }
        zipFile.close();
    } catch (IOException e) {
        //LOG.error("[compress]", e);
        throw new RuntimeException("[compress]", e);
    }
}

From source file:herddb.upgrade.ZIPUtils.java

public static List<File> unZip(InputStream fs, File outDir) throws IOException {

    try (ZipInputStream zipStream = new ZipInputStream(fs, StandardCharsets.UTF_8);) {
        ZipEntry entry = zipStream.getNextEntry();
        List<File> listFiles = new ArrayList<>();
        while (entry != null) {
            if (entry.isDirectory()) {
                entry = zipStream.getNextEntry();
                continue;
            }//  w  ww.  j av a 2  s.  c o m

            String normalized = normalizeFilenameForFileSystem(entry.getName());
            File outFile = new File(outDir, normalized);
            File parentDir = outFile.getParentFile();
            if (parentDir != null && !parentDir.isDirectory()) {
                Files.createDirectories(parentDir.toPath());
            }

            listFiles.add(outFile);
            try (FileOutputStream out = new FileOutputStream(outFile);
                    SimpleBufferedOutputStream oo = new SimpleBufferedOutputStream(out)) {
                IOUtils.copyLarge(zipStream, oo);
            }
            entry = zipStream.getNextEntry();

        }
        return listFiles;
    } catch (IllegalArgumentException ex) {
        throw new IOException(ex);
    }

}