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

public static final void zipDirectory(File directory, File zip) throws IOException {
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
    zip(directory, directory.getParentFile(), zos);
    zos.close();//from  w  w  w  .  ja  v  a  2  s  .  c  o  m
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a zip entry in a directory/*from w ww. j  a  v  a2s .  c om*/
 * 
 * @param zipfile
 * @param entry
 * @param outputDir
 * @throws IOException
 */
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        ClosingUtils.close(outputStream);
        ClosingUtils.close(inputStream);
    }
}

From source file:Main.java

public static boolean isSymboliclink(File file) throws IOException {
    File canon;/*ww w .j  a  va  2s  .c  o m*/
    if (file.getParent() == null) {
        canon = file;
    } else {
        File canonDir = file.getParentFile().getCanonicalFile();
        canon = new File(canonDir, file.getName());
    }
    return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}

From source file:Main.java

/**
 * Save {@link String} to {@link File} witht the specified encoding
 *
 * @param string {@link String}/*from   www  . j a  va 2s . co  m*/
 * @param path   Path of the file
 * @param string Encoding
 * @throws IOException
 */
public static void saveStringToFile(String string, File path, String encoding) throws IOException {
    if (path.exists()) {
        path.delete();
    }

    if ((path.getParentFile().mkdirs() || path.getParentFile().exists())
            && (path.exists() || path.createNewFile())) {
        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding));
        writer.write(string);
        writer.close();
    }
}

From source file:Main.java

/**
 * Method to split the path components of a file into a List
 * @param f the file to split/* ww w. java2 s.  co m*/
 * @return a new list containing the components of the path as strings
 */
protected static List<String> splitPath(File f) {
    List<String> result;
    File parent = f.getParentFile();
    if (parent == null) {
        result = new ArrayList<String>();
        if (f.getName().length() > 0) {
            // We're at the root but have a name so we have a relative path
            // for which we need to add the first component to the list
            result.add(f.getName());
        } else if (IS_WINDOWS) {
            String strF = f.toString();
            if (strF.matches(WINDOWS_DRIVE_ROOT_REGEX)) {
                // We're on windows and the path is <Drive>:\ so we
                // add the <Drive>: to the list
                result.add(strF.substring(0, strF.length() - 1).toUpperCase());
            }
        }
    } else {
        result = splitPath(parent);
        result.add(f.getName());
    }
    return result;
}

From source file:net.amigocraft.mpt.command.RemoveCommand.java

public static void checkParent(File file) {
    if (file != null) {
        if (file.getParentFile().listFiles().length == 0) {
            file.getParentFile().delete();
            checkParent(file.getParentFile());
        }// w  w  w  .jav a 2s.  co m
    } else
        throw new IllegalArgumentException("File cannot be null!");
}

From source file:Main.java

public static void saveImage(String imagePath, byte[] buffer) throws IOException {
    File f = new File(imagePath);
    if (f.exists()) {
        return;//ww  w.  ja v  a 2 s .  c o m
    } else {
        File parentFile = f.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(imagePath);
        fos.write(buffer);
        fos.flush();
        fos.close();
    }
}

From source file:com.avatarproject.core.storage.UserCache.java

private static void create(File file) {
    if (!file.getParentFile().exists()) {
        try {/*from   ww w  .  ja v a2s . co  m*/
            file.getParentFile().mkdirs();
        } catch (Exception e) {
            AvatarProjectCore.get().getLogger().info("Failed to generate directory!");
            e.printStackTrace();
        }
    }

    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (Exception e) {
            AvatarProjectCore.get().getLogger().info("Failed to generate " + file.getName() + "!");
            e.printStackTrace();
        }
    }
}

From source file:Main.java

private static long copyLarge(InputStream input, File outputFile) throws IOException {
    byte[] buffer = new byte[16384];
    long count = 0;
    int n = 0;/*from w  w  w  . j ava  2  s . com*/
    outputFile.getParentFile().mkdirs();
    FileOutputStream output = new FileOutputStream(outputFile);
    try {
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
    } finally {
        output.close();
    }
    return count;
}

From source file:com.digitalreasoning.herman.JarCreater.java

public static void createJar(File outputJarFile, List<Entry> entries) throws IOException {
    if (!outputJarFile.getParentFile().exists()) {
        outputJarFile.getParentFile().mkdirs();
    }/*from w w  w  .ja  v a2s  . c  o m*/
    if (!outputJarFile.exists()) {
        outputJarFile.createNewFile();
    }

    FileOutputStream fOut = new FileOutputStream(outputJarFile);
    JarOutputStream jarOut = new JarOutputStream(fOut);
    Set<String> packageSet = new HashSet<String>();
    try {
        for (Entry folderFile : entries) {
            InputStream inputStream = folderFile.resource.openStream();

            try {
                if (!packageSet.contains(folderFile.parentFolderName)) {
                    jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName));
                    jarOut.closeEntry();
                    packageSet.add(folderFile.parentFolderName);
                }

                jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName
                        + (folderFile.parentFolderName.endsWith("/") ? "" : "/") + folderFile.fileName));
                IOUtils.copy(inputStream, jarOut);
                jarOut.closeEntry();
            } finally {
                inputStream.close();
            }

        }

    } finally {
        jarOut.close();
        fOut.close();
    }
}