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.bhbsoft.videoconference.record.convert.util.FlvFileHelper.java

public static File appendSuffix(File original, String suffix) {
    File parent = original.getParentFile();
    String name = original.getName();
    String ext = "";
    int idx = name.lastIndexOf('.');
    if (idx > -1) {
        name = name.substring(0, idx);/*from  w  ww  . ja va 2 s .com*/
        ext = name.substring(idx);
    }
    return new File(parent, name + suffix + ext);
}

From source file:mSearch.tool.Functions.java

public static String getPathJar() {
    // liefert den Pfad der Programmdatei mit File.separator am Schluss
    String pFilePath = "version.properties";
    File propFile = new File(pFilePath);
    if (!propFile.exists()) {
        try {//ww w  . j  ava2s.co m
            CodeSource cS = Main.class.getProtectionDomain().getCodeSource();
            File jarFile = new File(cS.getLocation().toURI().getPath());
            String jarDir = jarFile.getParentFile().getPath();
            propFile = new File(jarDir + File.separator + pFilePath);
        } catch (Exception ignored) {
        }
    } else {
        DbgMsg.print("getPath");
    }
    String s = propFile.getAbsolutePath().replace(pFilePath, "");
    if (!s.endsWith(File.separator)) {
        s = s + File.separator;
    }
    if (s.endsWith("/lib/")) {
        // dann sind wir in der msearch-lib
        s = s.replace("/lib/", "");
    }
    return s;
}

From source file:com.betfair.cougar.codegen.FileUtil.java

/**
 * Copy the given resource to the given file.
 *
 * @param resourceName name of resource to copy
 * @param destination file//w  w  w. jav a2  s  . c o  m
 */
public static void resourceToFile(String resourceName, File dest, Class src) {

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.getClassLoader().getResourceAsStream(resourceName);
        if (is == null) {
            throw new RuntimeException("Could not load resource: " + resourceName);
        }
        dest.getParentFile().mkdirs();
        os = new FileOutputStream(dest);

        IOUtils.copy(is, os);

    } catch (Exception e) {
        throw new RuntimeException(
                "Error copying resource '" + resourceName + "' to file '" + dest.getPath() + "': " + e, e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.liberty.android.fantastischmemo.downloader.DownloaderUtils.java

public static File downloadFile(String url, String savedPath) throws IOException {
    File outFile = new File(savedPath);
    OutputStream out;// w  ww  .  j  av a2 s  . c o  m

    // Delete and backup if the file exists
    AMUtil.deleteFileWithBackup(savedPath);

    // Create the dir if necessary
    File parentDir = outFile.getParentFile();
    parentDir.mkdirs();

    outFile.createNewFile();
    out = new FileOutputStream(outFile);

    Log.i(TAG, "URL to download is: " + url);
    URL myURL = new URL(url);
    URLConnection ucon = myURL.openConnection();
    byte[] buf = new byte[8192];

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is, 8192);
    int len = 0;
    while ((len = bis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    is.close();
    return outFile;
}

From source file:com.aoyetech.fee.commons.utils.FileUtils.java

public static File createFileIfNessary(final String path) throws IOException {
    final File file = new File(path);
    final File parentFile = file.getParentFile();
    if (!parentFile.exists()) {
        createDirsIfNessary(parentFile.getAbsolutePath());
    }//from w  ww  .  ja  va 2 s  . c  o m
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (final Exception e) {
            logger.error(", path=" + path, e);
        }
    }
    return file;
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*  w  w w  .j  av a  2s  . c o  m*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    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
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();

}

From source file:Main.java

public static boolean createFile(File file, boolean forceCreate) throws IOException {
    if (file == null) {
        return false;
    }/*from  w  w  w.  jav a2s  .co  m*/

    if (file.exists()) {
        if (!forceCreate) {
            return false;
        } else {
            file.delete();
        }
    }

    if (!isExistDirectory(file.getParentFile().getAbsolutePath())) {
        file.getParentFile().mkdirs();
    }

    return file.createNewFile();
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeVfsTestCase.java

public static void assertEquals(File file, FileObject fileObject) throws Exception {
    assertEqualPaths(file, fileObject);/*from   ww  w  .  jav a 2 s  .c  o  m*/
    assertEquals(file.canRead(), fileObject.isReadable());
    assertEquals(file.canWrite(), fileObject.isWriteable());
    assertEquals(file.exists(), fileObject.exists());
    if (file.getParentFile() == null) {
        assertNull(fileObject.getParent());
    } else {
        assertEqualPaths(file.getParentFile(), fileObject.getParent());
    }
    assertEquals(file.isDirectory(), fileObject.getType().hasChildren());
    assertEquals(file.isFile(), fileObject.getType().hasContent());
    assertEquals(file.isHidden(), fileObject.isHidden());
    if (file.isFile()) {
        assertEquals(file.length(), fileObject.getContent().getSize());
    }
    if (file.isDirectory()) { // same children
        File[] childFiles = file.listFiles();
        FileObject[] childObjects = fileObject.getChildren();
        assertEquals(childFiles.length, childObjects.length);
        for (int i = 0; i < childFiles.length; i++) {
            assertEqualPaths(childFiles[i], childObjects[i]);
        }
    }
}

From source file:com.moss.posixfifosockets.PosixFifoSocket.java

public static void createFifo(File path) throws IOException {
    if (path.exists()) {
        throw new IOException("File already exists: " + path.getAbsolutePath());
    }/*from  w w w  . j  a  va2  s . c o  m*/
    if (!path.getParentFile().exists()) {
        throw new FileNotFoundException(path.getParent());
    }
    try {
        if (path.exists()) {
            throw new RuntimeException("Path really does exist: " + path.getAbsolutePath());
        }

        final Process p = Runtime.getRuntime().exec("mkfifo " + path.getAbsolutePath());
        int result = p.waitFor();
        if (result != 0) {

            String stdOut = read(p.getInputStream());
            String stdErr = read(p.getErrorStream());
            throw new IOException("Error creating fifo at " + path.getAbsolutePath() + ": Received error code "
                    + result + ", STDOUT: " + stdOut + ", STDERR: " + stdErr);
        } else if (!path.exists()) {
            throw new RuntimeException("mkfifo didn't do its job: " + path.getAbsolutePath());
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sketchy.utils.image.SketchyImage.java

public static void save(SketchyImage sketchyImage, File file) throws Exception {
    if (!file.getParentFile().canWrite()) {
        throw new Exception("Can not write to File: " + file.getPath() + "!");
    }/* w ww.  j ava2  s.  c  om*/

    if (!StringUtils.endsWithIgnoreCase(file.getName(), ".png")) {
        throw new Exception("Can not save SketchyImage! Must be a .png file!");
    }

    Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("png");
    ImageWriter imageWriter = null;
    if (imageWriters.hasNext()) { // Just get first one
        imageWriter = imageWriters.next();
    }
    if (imageWriter == null) {
        // this should never happen!! if so.. we got problems
        throw new Exception("Can not find ImageReader for .png Files!");
    }

    ImageOutputStream os = null;
    try {
        os = ImageIO.createImageOutputStream(file);
        imageWriter.setOutput(os);

        ImageWriteParam imageWriterParam = imageWriter.getDefaultWriteParam();
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(
                ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY),
                imageWriterParam);

        String metaDataFormatName = metadata.getNativeMetadataFormatName();
        IIOMetadataNode metaDataNode = (IIOMetadataNode) metadata.getAsTree(metaDataFormatName);

        NodeList childNodes = metaDataNode.getElementsByTagName("pHYs");
        IIOMetadataNode physNode = null;
        if (childNodes.getLength() == 0) {
            physNode = new IIOMetadataNode("pHYs");
            physNode.setAttribute("pixelsPerUnitXAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
            physNode.setAttribute("pixelsPerUnitYAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
            physNode.setAttribute("unitSpecifier", "meter"); // always meter
            metaDataNode.appendChild(physNode);
        } else {
            for (int nodeIdx = 0; nodeIdx < childNodes.getLength(); nodeIdx++) {
                physNode = (IIOMetadataNode) childNodes.item(nodeIdx);
                physNode.setAttribute("pixelsPerUnitXAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
                physNode.setAttribute("pixelsPerUnitYAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
                physNode.setAttribute("unitSpecifier", "meter"); // always meter
                metaDataNode.appendChild(physNode);
            }
        }
        metadata.setFromTree(metaDataFormatName, metaDataNode);
        imageWriter.write(new IIOImage(sketchyImage.image, null, metadata));
        os.flush();
    } catch (Exception e) {
        throw new Exception("Error Saving SketchyImage File: " + file.getPath() + "!  " + e.getMessage());
    } finally {
        IOUtils.closeQuietly(os);
    }
}