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 saveFileForLocal(String requestPath, String result) {
    // TODO Auto-generated method stub
    File file = new File(requestPath);
    if (!file.exists()) {
        try {/*from w w  w .  j a v a 2  s.  co m*/
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            file.createNewFile();
            FileOutputStream fout = new FileOutputStream(file);
            byte[] buffer = result.getBytes();
            fout.write(buffer);
            fout.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.microsoft.tfs.client.common.ui.framework.diagnostics.InternalSupportUtils.java

public static void openFolderOfFile(final File file) {
    if (file.getParentFile() != null) {
        Launcher.launch(file.getParentFile().getAbsolutePath());
    }/*w  w  w.  j  a va 2s .  c  o  m*/
}

From source file:Main.java

public static File getClassPathFile(Class clazz) {
    File file = getClassFile(clazz);
    for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++)
        file = file.getParentFile();
    if (file.getName().toUpperCase().endsWith(".JAR!")) {
        file = file.getParentFile();/*from  www  . java2 s  . co  m*/
    }
    return file;
}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;
    file = new File(path);
    if (!file.exists()) {
        try {//from   w  w w.j  av  a2  s.  c  o m
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.splout.db.common.CompressorUtil.java

public static void uncompress(File file, File dest) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(dest, entry.getName());
        entryDestination.getParentFile().mkdirs();
        InputStream in = zipFile.getInputStream(entry);
        OutputStream out = new FileOutputStream(entryDestination);
        IOUtils.copy(in, out);/*from   w  w  w.  j  a v  a 2  s. c  om*/
        in.close();
        out.close();
    }
}

From source file:egovframework.com.utl.wed.filter.DirectoryPathManager.java

public static File getUniqueFile(final File file) {
    if (!file.exists())
        return file;

    File tmpFile = new File(file.getAbsolutePath());
    File parentDir = tmpFile.getParentFile();
    int count = 1;
    String extension = FilenameUtils.getExtension(tmpFile.getName());
    String baseName = FilenameUtils.getBaseName(tmpFile.getName());
    do {//from w  w w . ja va  2  s  .  c om
        tmpFile = new File(parentDir, baseName + "_" + count++ + "_." + extension);
    } while (tmpFile.exists());
    return tmpFile;
}

From source file:info.mikaelsvensson.devtools.common.PathUtils.java

public static String getRelativePath(File source, File target) {
    source = source.isDirectory() ? source : source.getParentFile();

    String sourceFixed = fixPath(source);
    String targetFixed = fixPath(target);

    String[] sourceParts = StringUtils.split(sourceFixed, SEP);
    String[] targetParts = StringUtils.split(targetFixed, SEP);
    int sharedParts = 0;
    for (int i = 0; i < sourceParts.length; i++) {
        String sourcePart = sourceParts[i];
        if (targetParts.length == i) {
            break;
        }/*w w w .  j  a v  a2 s  .c o  m*/
        String targetPart = targetParts[i];
        if (sourcePart.equals(targetPart)) {
            sharedParts = i;
        }
    }

    String toSharedRoot = StringUtils.repeat(".." + SEP, sourceParts.length - sharedParts - 1);
    String fromSharedRoot = StringUtils.join(targetParts, SEP, sharedParts + 1, targetParts.length);
    if (StringUtils.isEmpty(toSharedRoot) && StringUtils.isEmpty(fromSharedRoot)) {
        return "." + SEP;
    } else {
        return toSharedRoot + fromSharedRoot;
    }

}

From source file:com.knewton.mapreduce.io.sstable.BackwardsCompatibleDescriptor.java

/**
 * Implementation of {@link Descriptor#fromFilename(String)} that is backwards compatible with
 * older sstables//from w  ww  .  j  av a  2s  .com
 *
 * @param filename
 * @return A descriptor for the sstable
 */
public static Descriptor fromFilename(String filename) {
    File file = new File(filename);
    return fromFilename(file.getParentFile(), file.getName()).left;
}

From source file:com.zb.app.common.file.FileUtils.java

public static void unJar(File jarFile, File toDir) throws IOException {
    JarFile jar = new JarFile(jarFile);
    try {//from  ww w. j  a  v  a2 s  .  c o  m
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = (JarEntry) entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = jar.getInputStream(entry);
                try {
                    File file = new File(toDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        jar.close();
    }
}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;//from w  w w . j a  v  a2s  .co  m
    PrintStream out = null;
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return -1;
    }
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdir();
    }
    try {
        out = new PrintStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        ret = -2;
    } finally {
        out.flush();
        out.close();
        if (!bitmap.isRecycled())
            bitmap.recycle();
    }

    return ret;
}