Example usage for java.io File getParent

List of usage examples for java.io File getParent

Introduction

In this page you can find the example usage for java.io File getParent.

Prototype

public String getParent() 

Source Link

Document

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

Usage

From source file:com.yqboots.fss.util.ZipUtils.java

/**
 * Compresses the specified directory to a zip file
 *
 * @param dir the directory to compress//from w  w  w . j  a  v  a2  s  .c  om
 * @return the compressed file
 * @throws IOException
 */
public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
        // out.setMethod(ZipOutputStream.DEFLATED);
        final byte data[] = new byte[BUFFER];
        // get a list of files from current directory
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                    throws IOException {
                final File file = path.toFile();

                // compress to relative directory, not absolute
                final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
                try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file),
                        BUFFER)) {
                    final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                }

                return FileVisitResult.CONTINUE;
            }
        });
    }

    return result;
}

From source file:muffinc.yafdivj.helper.FeretHandler.java

public static void rename(String s) {
    for (File file : new File(s).listFiles()) {
        if (file.isDirectory()) {
            int size = file.listFiles(((FileFilter) new WildcardFileFilter("*.tif"))).length;

            file.renameTo(new File(file.getParent() + "/" + file.getName() + "_" + size));

        }/*from  ww w. j a  v  a2 s.  co  m*/
    }
}

From source file:Main.java

/**
 * Returns a reference to a file with the specified name that is located
 * somewhere on the classpath. The code for this method is an adaptation of
 * code supplied by Dave Postill./*  w  w w  . j  a  v  a  2 s.c om*/
 * 
 * @param name
 *          the filename.
 * 
 * @return a reference to a file or <code>null if no file could be
 *         found.
 */
public static File findFileOnClassPath(final String name) {

    final String classpath = System.getProperty("java.class.path");
    final String pathSeparator = System.getProperty("path.separator");

    final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);

    while (tokenizer.hasMoreTokens()) {
        final String pathElement = tokenizer.nextToken();

        final File directoryOrJar = new File(pathElement);
        final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();

        if (absoluteDirectoryOrJar.isFile()) {
            final File target = new File(absoluteDirectoryOrJar.getParent(), name);
            if (target.exists()) {
                return target;
            }
        } else {
            final File target = new File(directoryOrJar, name);
            if (target.exists()) {
                return target;
            }
        }

    }
    return null;

}

From source file:gov.nih.nci.cacis.transform.SourceTransformerTest.java

@BeforeClass
public static void setupEnv() throws URISyntaxException {
    System.setProperty("cacis-pco.transformer.xml2rdf.xsl", "sampleXSLv2.xsl");
    final File smplxsl = new File(
            SourceTransformerTest.class.getClassLoader().getResource("xsl2/sampleXSLv2.xsl").toURI());
    System.setProperty("cacis-pco.transformer.xsl.baseClassPath", smplxsl.getParent() + "/");
}

From source file:Main.java

public static boolean compress(File file) {
    try {//from  w  w  w.ja  v a  2  s.c o m
        String fileName = file.getName();
        if (fileName.indexOf(".") != -1)
            fileName = fileName.substring(0, fileName.indexOf("."));
        FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName + ".zip");
        CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
        InputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getName()));
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);
        out.closeEntry();

        in.close();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.nubits.nubot.options.SaveOptions.java

/**
 * backup an options file. write it to .bak and increase counter
 *
 * @param filepath/*from  w  w w  . j  ava 2  s .co  m*/
 * @return
 */
public static boolean backupOptions(String filepath) throws IOException {
    File f = new File(filepath);
    if (f.exists()) {
        boolean wrote = false;
        int i = 0;
        while (!wrote) {
            String fp = f.getParent() + File.separator + f.getName() + "_" + i + ".bak";
            File dest = new File(fp);
            if (!dest.exists()) {
                try {
                    FileUtils.copyFile(f, dest);
                    return true;
                } catch (IOException e) {
                    throw new IOException(e);
                }
            }
            i++;

            if (i > 100)
                return false;
        }
    }
    return false;
}

From source file:jetbrains.exodus.io.FileDataReader.java

private static boolean renameFile(@NotNull final File file) {
    final String name = file.getName();
    return file.renameTo(new File(file.getParent(),
            name.substring(0, name.indexOf(LogUtil.LOG_FILE_EXTENSION)) + DELETED_FILE_EXTENSION));
}

From source file:maltcms.ui.fileHandles.properties.tools.SceneExporter.java

public static void saveScene(PipelineGraphScene scene, File configuration) {
    final SceneExporter exporter = new SceneExporter(
            configuration.getParent() + System.getProperty("file.separator"), configuration.getName(), scene);
    if (exporter.export()) {
        JOptionPane.showMessageDialog(scene.getView(), "Configuration saved!", "Confirmation", 1);
    } else {//w  ww.  j  a v a  2  s  .  c om
        JOptionPane.showMessageDialog(scene.getView(), "Configuration not saved! An Error Occured.",
                "Saving Failed", 1);
    }
}

From source file:com.athena.peacock.engine.action.ConfigurationActionTest.java

/**
 * <pre>//from   w  w w  .  ja  va  2 s.  com
 *     env.ori ?? env.sh ? .
 * </pre>
 * @throws java.lang.Exception
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    File file = new File(new File(ZipUtilTest.class.getResource("/peacock/configuration").getFile()),
            "env.ori");
    copy(file, new File(file.getParent() + File.separator + "env.sh"));
}

From source file:com.ccoe.build.core.utils.FileUtils.java

public static void renameFile(File file, String ext) {
    if (!file.exists()) {
        return;/*from ww w. ja v  a2s.  c  o  m*/
    }
    File dest = new File(file.getParent(), file.getName() + ext);
    boolean success = file.renameTo(dest);
    if (success) {
        System.out.println("[INFO] Rename Session LOG " + dest);
    } else {
        System.out.println("[WARNING] Failed rename session LOG to " + dest);
    }
}