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:de.tudarmstadt.ukp.dkpro.core.api.resources.CompressionUtils.java

/**
 * Make sure the target directory exists and get a stream writing to the specified file within.
 * If the file name ends with a typical extension for compressed files, the stream will be
 * compressed./*from  w  ww.  j  av  a  2  s .  c o m*/
 * 
 * @param aFile
 *            the target file.
 * @return a stream to write to.
 * 
 * @see CompressionMethod
 */
public static OutputStream getOutputStream(File aFile) throws IOException {
    // Create parent folders for output file and set up stream
    if (aFile.getParentFile() != null) {
        forceMkdir(aFile.getParentFile());
    }

    String lcFilename = aFile.getName().toLowerCase();

    OutputStream os = new FileOutputStream(aFile);
    if (lcFilename.endsWith(GZIP.getExtension())) {
        os = new GZIPOutputStream(os);
    } else if (lcFilename.endsWith(BZIP2.getExtension()) || lcFilename.endsWith(".bzip2")) {
        os = new BZip2CompressorOutputStream(os);
    } else if (lcFilename.endsWith(XZ.getExtension())) {
        os = new XZCompressorOutputStream(os);
    }
    return os;
}

From source file:com.twitter.heron.apiserver.utils.FileHelper.java

public static void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation)
        throws IOException {
    File file = new File(uploadedFileLocation);
    file.getParentFile().mkdirs();

    int read = 0;
    byte[] bytes = new byte[1024];

    try (OutputStream out = new FileOutputStream(file)) {
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);/*  w  ww .j a  v a 2s .  c  o  m*/
        }
        out.flush();
    }
}

From source file:com.justgiving.raven.kissmetrics.utils.KissmetricsLocalSchemaExtractor.java

/****
 * This function parses all the json record files in a folder and returns a counts of the total occurrences of keys
 * in all files// w  w w  . j  a  v  a 2  s.  co  m
 * 
 * @param inputFolder
 * @param outputFolder
 * @throws IOException
 */
private static void countKeysInJsonRecordsFolder(String inputFolder, String outputFile) throws IOException {
    File folder = new File(inputFolder);
    File[] listOfFiles = folder.listFiles();
    KeyValueCounter totalKeyValueCounter = new KeyValueCounter();
    KeyValueCounter currentKeyValueCounter = new KeyValueCounter();
    for (File currentFile : listOfFiles) {
        if (currentFile.isFile()) {
            logger.info("Processing file: " + currentFile.getName());
            currentKeyValueCounter = countKeysInJsonRecordsFile(
                    Paths.get(inputFolder, currentFile.getName()).toString());
            totalKeyValueCounter = deepMergeKeyValueCounter(totalKeyValueCounter, currentKeyValueCounter);
        } else if (currentFile.isDirectory()) {
            logger.warn("Sub-directory folders are currently ignored");
        }
    }
    //System.out.println(totalKeyCounter.toString());
    logger.info("---------------");
    logger.info(sortOutputByKey(totalKeyValueCounter));
    logger.info("saving output to file: ");
    File outpuFile = new File(outputFile);
    outpuFile.getParentFile().mkdirs();
    PrintWriter out = new PrintWriter(outputFile);
    out.print(sortOutputByKey(totalKeyValueCounter));
    out.close();
}

From source file:com.liusoft.dlog4j.TextCacheManager.java

/**
 * /*from  w  w  w.j ava2 s.c  om*/
 * @param type
 * @param id
 * @param text
 */
public static void updateTextContent(int type, long id, String text) {
    if (text == null)
        return;
    String path = getFilePath(type, id);
    FileOutputStream fos = null;
    try {
        File f = new File(path);
        if (!f.getParentFile().exists())
            f.getParentFile().mkdirs();
        fos = new FileOutputStream(f);
        fos.write(text.getBytes());
    } catch (IOException e) {
        log.error("Exception occur when writing to " + path, e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToZipFile(Object object, File file) throws IOException {
    if (!file.exists()) {
        file.getParentFile().mkdirs();
    }/*from  www .j  a v a 2  s  .com*/
    writeObjectToZipStream(object, new FileOutputStream(file));
}

From source file:com.dev.pygmy.util.Utils.java

/**
 * Saves the current game by serializing it and storing in locally
 * on the device// w  w  w.j av  a 2s .  c om
 */
public static void saveGame(PygmyGame game, String path) {
    ObjectOutputStream oos = null;
    try {
        File history = new File(path);
        history.getParentFile().createNewFile();
        FileOutputStream fout = new FileOutputStream(history);
        oos = new ObjectOutputStream(fout);
        oos.writeObject(game);
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.flush();
                oos.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.exalttech.trex.stateful.utilities.FileManager.java

/**
 * Copy the select file to the local directory
 *
 * @param srcFile/*from  w w  w. j  av a  2s. c  o m*/
 * @return
 * @throws IOException
 */
public static File copyFile(File srcFile) throws IOException {
    File destFile;
    String srcFileDirectory = "";
    if (srcFile.getParentFile() != null) {
        srcFileDirectory = srcFile.getParentFile().getName();
    }
    destFile = new File(createDirectoryIfNotExists(getLocalFilePath() + srcFileDirectory) + File.separator
            + srcFile.getName());
    FileUtils.copyFile(srcFile, destFile);
    return destFile;
}

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

/**
 * The path to the file must exist.//from   w  w w. jav  a  2 s .  c  o  m
 * @param jp
 * @param path
 * @throws JRException
 * @throws FileNotFoundException
 */
public static void exportReportPDF(JasperPrint jp, String path)
        throws JRException, FileNotFoundException, IOException {
    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();

    fos.close();

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

From source file:Main.java

public static void saveImage(String imagePath, Bitmap bm) {

    if (bm == null || imagePath == null || "".equals(imagePath)) {
        return;/*from ww  w.j a  va  2 s. c o  m*/
    }

    File f = new File(imagePath);
    if (f.exists()) {
        return;
    } else {
        try {
            File parentFile = f.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            f.createNewFile();
            FileOutputStream fos;
            fos = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            f.delete();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            f.delete();
        }
    }
}

From source file:msec.org.TarUtil.java

private static void fileProber(File dirFile) {

    File parentFile = dirFile.getParentFile();
    if (!parentFile.exists()) {

        // /* ww w . j  a  v  a 2  s .  c o  m*/
        fileProber(parentFile);

        parentFile.mkdir();
    }

}