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:Main.java

private static void fileUnZip(ZipInputStream zis, File file) throws FileNotFoundException, IOException {
    java.util.zip.ZipEntry zip = null;
    while ((zip = zis.getNextEntry()) != null) {
        String name = zip.getName();
        File f = new File(file.getAbsolutePath() + File.separator + name);
        if (zip.isDirectory()) {
            f.mkdirs();/*from w  w  w.  ja  v  a 2  s. co m*/
        } else {
            f.getParentFile().mkdirs();
            f.createNewFile();
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(f));
                byte b[] = new byte[2048];
                int aa = 0;
                while ((aa = zis.read(b)) != -1) {
                    bos.write(b, 0, aa);
                }
                bos.flush();
            } finally {
                bos.close();
            }
            bos.close();
        }
    }

}

From source file:es.emergya.tools.JarSearcher.java

/**
 * Returns the path to the .jar-Datei containing the class of the object
 * given//from   ww  w  .  j  a v  a2s.  c o  m
 * 
 * @param obj
 * @return <code>null</code> if the class of the object is not in a .jar
 *         file
 */
public static File getJarDirectory(Object obj) {
    // Search for .jar file
    File jar = JarSearcher.getJar(obj);
    if (jar != null)
        // .jar found
        return jar.getParentFile();
    // ... not found
    return null;
}

From source file:Main.java

public static void copyfile(String fromFilePath, String toFilePath, Boolean rewrite) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
        return;// ww w .  j a  v a  2 s.com
    }

    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }

    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        FileInputStream fosfrom = new FileInputStream(fromFile);
        FileOutputStream fosto = new FileOutputStream(toFile);

        byte[] bt = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 *
 *
 * @param context//ww  w .ja  v  a 2s  .co m
 * @param assetFilename
 * @param out
 * @throws IOException
 */
public static void copy(Context context, String assetFilename, File out) throws IOException {
    if (context == null) {
        return;
    } else if (out == null) {
        return;
    }

    if (!out.getParentFile().exists()) {
        out.getParentFile().mkdirs();
    }

    AssetManager assetManager = context.getAssets();
    InputStream is = null;
    FileOutputStream fos = null;
    try {
        is = assetManager.open(assetFilename);
        fos = new FileOutputStream(out);
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = is.read(buf, 0, 1024)) != -1) {
            fos.write(buf, 0, len);
        }
    } finally {
        if (fos != null)
            fos.close();

        if (is != null)
            is.close();
    }
}

From source file:com.playonlinux.core.utils.Files.java

public static boolean isInSubDirectory(File directory, File fileIside) {
    return fileIside != null
            && (fileIside.equals(directory) || isInSubDirectory(directory, fileIside.getParentFile()));
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java

/**
 * Extracts the specified folder from the specified archive, into the supplied output directory.
 * //from  w w  w . j  a  va 2 s. c o  m
 * @param archivePath
 *          the archive Path
 * @param folderToExtract
 *          the folder to extract
 * @param outputDirectory
 *          the output directory
 * @return the extracted folder path
 * @throws IOException
 *           if a problem occurs while extracting
 */
public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract,
        String outputDirectory) throws IOException {
    File destinationFolder = new File(outputDirectory);
    destinationFolder.mkdirs();

    ZipFile zip = null;
    try {
        zip = new ZipFile(new File(archivePath));
        Enumeration<?> zipFileEntries = zip.entries();
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.startsWith(folderToExtract)) {
                File destFile = new File(destinationFolder, currentEntry);
                destFile.getParentFile().mkdirs();
                if (!entry.isDirectory()) {
                    BufferedInputStream is = null;
                    BufferedOutputStream dest = null;
                    try {
                        is = new BufferedInputStream(zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER_SIZE];

                        // write the current file to disk
                        FileOutputStream fos = new FileOutputStream(destFile);
                        dest = new BufferedOutputStream(fos, BUFFER_SIZE);

                        // read and write until last byte is encountered
                        while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                    } finally {
                        if (dest != null) {
                            dest.flush();
                        }
                        IOUtils.closeQuietly(dest);
                        IOUtils.closeQuietly(is);
                    }
                }
            }
        }
    } finally {
        if (zip != null) {
            zip.close();
        }
    }

    return new File(destinationFolder, folderToExtract);
}

From source file:Main.java

public static boolean renameFile(File file, String newFileName) {
    if (newFileName.matches(FILENAME_REGIX)) {
        File newFile = null;//from www  . j  a v  a  2 s. c om
        if (file.isDirectory()) {
            newFile = new File(file.getParentFile(), newFileName);
        } else {
            String temp = newFileName + file.getName().substring(file.getName().lastIndexOf('.'));
            newFile = new File(file.getParentFile(), temp);
        }
        if (file.renameTo(newFile)) {
            return true;
        }
    }
    return false;
}

From source file:ImageUtil.java

/**
 * store BufferedImage to file/*from w ww .  j  a  va2s .  c o m*/
 * @param image BufferedImage
 * @param outputFile output image file
 * @param quality quality of output image
 * @return true success, else fail
 */
public static boolean storeImage(BufferedImage image, File outputFile, float quality) {
    try {
        //reconstruct folder structure for image file output
        if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        if (outputFile.exists()) {
            outputFile.delete();
        }
        //get image file suffix
        String extName = "png";
        //get registry ImageWriter for specified image suffix
        Iterator writers = ImageIO.getImageWritersByFormatName(extName);
        ImageWriter imageWriter = (ImageWriter) writers.next();
        //set image output params
        ImageWriteParam params = new JPEGImageWriteParam(null);
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionQuality(quality);
        params.setProgressiveMode(javax.imageio.ImageWriteParam.MODE_DISABLED);
        params.setDestinationType(new ImageTypeSpecifier(IndexColorModel.getRGBdefault(),
                IndexColorModel.getRGBdefault().createCompatibleSampleModel(16, 16)));
        //writer image to file
        ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputFile);
        imageWriter.setOutput(imageOutputStream);
        imageWriter.write(null, new IIOImage(image, null, null), params);
        imageOutputStream.close();
        imageWriter.dispose();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Creates parent directory for file if required.
 *
 * @param file/* ww w  .  jav  a 2 s .  c  o m*/
 */
public static void prepareDirs(final File file) {
    if (file.getParentFile() != null && !file.getParentFile().exists()) {
        prepareDirs(file.getParentFile());
        file.getParentFile().mkdirs();
    }
}

From source file:com.ibm.soatf.tool.FileSystem.java

public static String getRelativePath(File file) {
    try {//from w  ww.  j a va 2 s .  c o  m
        final String filePath = file.getCanonicalPath();
        final String testPath = file.getParentFile().getParentFile().getParent();
        if (filePath.startsWith(testPath)) {
            return filePath.substring(testPath.length() + 1);
        } else {
            return null;
        }
    } catch (IOException e) {
        ;
    }
    return file.getAbsolutePath();
}