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:cn.guoyukun.spring.web.upload.FileUploadUtils.java

private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException {

    uploadDir = FilenameUtils.normalizeNoEndSeparator(uploadDir);

    File desc = new File(uploadDir + File.separator + filename);

    if (!desc.getParentFile().exists()) {
        desc.getParentFile().mkdirs();/*from w w  w  .ja  v a 2 s .  c om*/
    }
    if (!desc.exists()) {
        desc.createNewFile();
    }
    return desc;
}

From source file:net.sf.jvifm.ui.Util.java

public static void openFileWithDefaultApp(String path) {
    if (path == null)
        return;//from w w  w. j a v a2s .  c om
    String ext = FilenameUtils.getExtension(path);
    File file = new File(path);
    if (ext.equals("bat") || ext.equals("sh")) {
        try {
            Runtime.getRuntime().exec(new String[] { path }, null, file.getParentFile());
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Program.launch(path);
    }

}

From source file:org.cloudfoundry.client.lib.SampleProjects.java

private static void unpackZip(ZipFile zipFile, File unpackDir) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File destination = new File(unpackDir.getAbsolutePath() + "/" + entry.getName());
        if (entry.isDirectory()) {
            destination.mkdirs();//ww w .j  ava  2s  . com
        } else {
            destination.getParentFile().mkdirs();
            FileCopyUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(destination));
        }
        if (entry.getTime() != -1) {
            destination.setLastModified(entry.getTime());
        }
    }
}

From source file:com.thoughtworks.go.util.FileUtil.java

public static boolean isSymbolicLink(File parent, String name) throws IOException {
    if (parent == null) {
        File f = new File(name);
        parent = f.getParentFile();
        name = f.getName();/*ww w .j  a va 2 s.c  om*/
    }
    File toTest = new File(parent.getCanonicalPath(), name);
    return !toTest.getAbsolutePath().equals(toTest.getCanonicalPath());
}

From source file:com.log4ic.compressor.utils.FileUtils.java

/**
 * ?//  www.  j  a  v a2  s.  c  om
 *
 * @param content
 * @param filePath
 * @return
 */
public static File writeFile(byte[] content, String filePath) {
    FileOutputStream out = null;
    FileChannel outChannel = null;
    File file = new File(filePath);

    if (file.exists()) {
        file.delete();
    }

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

    ByteBuffer outBuffer = ByteBuffer.allocate(content.length);
    outBuffer.put(content);
    outBuffer.flip();
    try {
        out = new FileOutputStream(file);

        outChannel = out.getChannel();

        outChannel.write(outBuffer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outChannel != null) {
            try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return file.exists() ? file : null;
}

From source file:eu.freme.eservices.epublishing.Unzipper.java

public static void unzip(ZipInputStream zis, File outputFolder) throws IOException {
    //create output directory is not exists

    if (!outputFolder.exists() && !outputFolder.mkdirs()) {
        throw new IOException("Cannot create directory " + outputFolder);
    }/*from   www .ja  v a 2 s  .c  o m*/

    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder, fileName);

        logger.debug("file unzip : " + newFile.getAbsoluteFile());

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        File parentDir = newFile.getParentFile();
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            throw new IOException("Cannot create directory " + newFile.getParent());
        }

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {
            try (FileOutputStream fos = new FileOutputStream(newFile)) {
                IOUtils.copyLarge(zis, fos);
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
}

From source file:com.eatnumber1.util.io.FileUtils.java

@NotNull
private static List<File> getFileElements(@NotNull File f) {
    List<File> cachedDirs = fileElementCache.get(f);
    if (cachedDirs != null)
        return cachedDirs;
    List<File> dirs = new LinkedList<File>();
    File dir = f.getParentFile();
    while (dir != null) {
        dirs.add(dir);/*  w  w  w. ja  v a2 s. c o  m*/
        dir = dir.getParentFile();
    }
    Collections.reverse(dirs);
    dirs = Collections.unmodifiableList(dirs);
    fileElementCache.put(f, dirs);
    return dirs;
}

From source file:com.gs.obevo.db.testutil.DirectoryAssert.java

public static String getRelativePath(File childFile, File baseFile) {
    if (childFile == null) {
        throw new IllegalArgumentException("childFile was not a child of the base file");
    } else if (childFile.equals(baseFile)) {
        return "";
    } else {/*from   w w  w. j a v  a 2 s.  c om*/
        return getRelativePath(childFile.getParentFile(), baseFile) + "/" + childFile.getName();
    }
}

From source file:com.appspresso.api.fs.FileSystemUtils.java

/**
 * InputStream  path?  ? ./*from   w w w. j  ava 2 s .co  m*/
 * 
 * @param inputStream ? InputStream
 * @param destFilePath  ??  path
 * @param overwrite  path? ? ??   ? 
 * @return ? {@literal true},   {@literal false}
 * @throws IOException ??   ?.
 */
public static boolean copy(InputStream inputStream, String destFilePath, boolean overwrite) throws IOException {
    File destFile = new File(destFilePath);
    File parent = destFile.getParentFile();
    if (!parent.exists() && !parent.mkdirs() && !parent.mkdir()) {
        return false;
    }

    if (destFile.exists()) {
        if (!overwrite) {
            return false;
        }
        if (!destFile.delete()) {
            return false;
        }
    }

    if (!destFile.createNewFile()) {
        return false;
    }

    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(destFile);
        BufferedOutputStream bOutputStream = new BufferedOutputStream(outputStream);

        byte[] buffer = new byte[BUFFER_SIZE];
        int length = -1;
        while ((length = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
            bOutputStream.write(buffer, 0, length);
        }
        bOutputStream.flush();
        return true;
    } finally {
        closeQuietly(inputStream);
        closeQuietly(outputStream);
    }
}

From source file:Main.java

public static boolean createFile(File file, boolean recursion) throws IOException {
    boolean result = false;
    if (!file.exists()) {
        try {//from ww w  .ja v  a2  s  .c  o  m
            result = file.createNewFile();
        } catch (IOException e) {
            if (!recursion) {
                throw e;
            }
            File parent = file.getParentFile();
            if (!parent.exists())
                parent.mkdirs();
            try {
                result = file.createNewFile();
            } catch (IOException e1) {
                throw e1;
            }
        }
    }
    return result;
}