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:cz.cas.lib.proarc.common.export.mets.JhoveUtility.java

/**
 * Copy the Jhove configuration file to a temporary file.
 *
 * @return the {@link File} where the Jhove configuration was saved.
 *
 *///  w  w w  .j a  va2s . co  m
private static File createJhoveConfigurationFile(File configFolder) throws MetsExportException {
    URL jhoveConf = JhoveUtility.class.getResource(JHOVE_CONFIG_NAME);
    URL jhoveConfXsd = JhoveUtility.class.getResource("jhoveConfig.xsd");
    try {
        File jhoveConfFile = new File(configFolder, JHOVE_CONFIG_NAME);
        LOG.log(Level.FINE, "JHOVE configuration file " + jhoveConfFile);
        if (!jhoveConfFile.exists()) {
            FileUtils.copyURLToFile(jhoveConf, jhoveConfFile);
        }
        File xsdFile = new File(jhoveConfFile.getParent(), "jhoveConfig.xsd");
        if (!xsdFile.exists()) {
            FileUtils.copyURLToFile(jhoveConfXsd, xsdFile);
        }
        return jhoveConfFile;
    } catch (IOException ex) {
        throw new MetsExportException("Unable to create jHove config file", false, ex);
    }
}

From source file:com.ebixio.virtmus.stats.StatsLogger.java

/**
 * Gzips log files. It is safe to run this more than once on the same directory.
 * @param logsDir The directory to search for un-zipped logs.
 * @param logSet The log set to zip up./* w ww  . j  a v a 2 s  . c om*/
 */
private static void gzipLogs(File logsDir, final String logSet) {
    FilenameFilter logFilter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            // VirtMus-A-0.log, VirtMus-A-0.log.1
            return Pattern.matches("VirtMus-" + logSet + "-\\d+\\.log(\\.\\d+)*", name);
        }
    };

    // Zip up log files to be uploaded
    int counter = 0;
    for (File f : logsDir.listFiles(logFilter)) {
        String newName;
        File newFile;
        do {
            newName = String.format("%s%sVirtMus-%03d.gz", f.getParent(), File.separator, ++counter);
            newFile = new File(newName);
        } while (newFile.exists());

        if (gzipFile(f, newName)) {
            f.delete();
        }
    }
}

From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java

/**
 * Uses reader and writer to perform copy
 * @param sourcefile/* w  w  w  . j a v  a  2s  . c  o  m*/
 * @param targetFile
 */
public static void copyTextFile(final File sourcefile, final File targetFile) {
    if (!sourcefile.exists()) {
        throw new RuntimeException("Sourcefile does not exist: " + sourcefile.getAbsolutePath());
    }
    if (!sourcefile.isFile()) {
        throw new RuntimeException("Sourcefile is no file: " + sourcefile.getAbsolutePath());
    }
    final String targetdir = targetFile.getParent();
    if (!targetFile.getParentFile().exists()) {
        throw new RuntimeException("TargetDirectory does not exist: " + targetdir);
    }

    BufferedReader in = null;
    BufferedWriter out = null;
    try {
        out = IOEncodingHelper.STANDARD.getBufferedWriter(targetFile);
        in = IOEncodingHelper.STANDARD.getBufferedReader(sourcefile);
        int c;

        while ((c = in.read()) != -1)
            out.write(c);
    } catch (Exception e) {
        throw new RuntimeException("Error copying " + sourcefile.getAbsolutePath() + " to " + targetdir, e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                throw new RuntimeException("Error closing reader " + in, e);
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException("Error writer reader " + out, e);
            }
        }
    }
}

From source file:mx.itesm.imb.EcoreImbEditor.java

/**
 * /*from www .  ja v a 2  s  .  c o m*/
 * @param templateProject
 */
@SuppressWarnings("unchecked")
public static void createRooApplication(final File ecoreProject, final File busProject,
        final File templateProject) {
    File imbProject;
    File editProject;
    String pluginContent;
    String pomContent;
    String tomcatConfiguration;
    Collection<File> pluginFiles;

    try {
        editProject = new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit");
        imbProject = new File(editProject, "/imb/");
        FileUtils.deleteDirectory(imbProject);

        // Create the roo application
        FileUtils.copyFile(new File(templateProject, "/templates/install.roo"),
                new File(imbProject, "install.roo"));
        EcoreImbEditor.executeCommand("roo script --file install.roo", imbProject);

        // Update libraries
        pomContent = FileUtils.readFileToString(new File(imbProject, "pom.xml"));
        pomContent = pomContent.replaceFirst("</dependencies>",
                FileUtils.readFileToString(new File(templateProject, "/templates/pom.xml")));
        FileUtils.writeStringToFile(new File(imbProject, "pom.xml"), pomContent);

        // IMB types configuration
        FileUtils.copyDirectory(new File(busProject, "/src/main/java/imb"),
                new File(imbProject, "/src/main/java/imb"));
        FileUtils.copyFile(new File(busProject, "/src/main/resources/schema.xsd"),
                new File(imbProject, "/src/main/resources/schema.xsd"));

        FileUtils.copyFile(
                new File(busProject,
                        "/src/main/resources/META-INF/spring/applicationContext-contentresolver.xml"),
                new File(imbProject,
                        "/src/main/resources/META-INF/spring/applicationContext-contentresolver.xml"));

        // Update the plugin configuration
        pluginFiles = FileUtils.listFiles(new File(editProject, "/src"), new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                return (file.getName().endsWith("Plugin.java"));
            }

            @Override
            public boolean accept(File dir, String file) {
                return (file.endsWith("Plugin.java"));
            }
        }, TrueFileFilter.INSTANCE);
        for (File plugin : pluginFiles) {
            pluginContent = FileUtils.readFileToString(plugin);
            pluginContent = pluginContent.substring(0,
                    pluginContent.indexOf("public static class Implementation extends EclipsePlugin"));

            // Tomcat configuration
            tomcatConfiguration = FileUtils
                    .readFileToString(new File(templateProject, "/templates/Plugin.txt"));
            tomcatConfiguration = tomcatConfiguration.replace("${imbProject}", imbProject.getPath());

            FileUtils.writeStringToFile(plugin, pluginContent + tomcatConfiguration);
            break;
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error while configuring Roo application: " + e.getMessage());
    }
}

From source file:com.discovery.darchrow.io.FileUtil.java

/**
 * ?????? null./*from   www  .  jav a 2  s  .  c  o m*/
 *
 * @param path
 *            the path
 * @return the parent
 * @see java.io.File#getParent()
 */
public static String getParent(String path) {
    if (Validator.isNullOrEmpty(path)) {
        throw new NullPointerException("pathname can't be null/empty!");
    }
    File file = new File(path);
    String parent = file.getParent();
    return parent;
}

From source file:com.silverpeas.util.ZipManager.java

/**
 * Mthode compressant un dossier de faon rcursive au format zip.
 *
 * @param folderToZip - dossier  compresser
 * @param zipFile - fichier zip  creer//from  w  w  w  .  j a  va2  s .  c om
 * @return la taille du fichier zip gnr en octets
 * @throws FileNotFoundException
 * @throws IOException
 */
public static long compressPathToZip(File folderToZip, File zipFile) throws IOException {
    ZipArchiveOutputStream zos = null;
    try {
        // cration du flux zip
        zos = new ZipArchiveOutputStream(new FileOutputStream(zipFile));
        zos.setFallbackToUTF8(true);
        zos.setCreateUnicodeExtraFields(NOT_ENCODEABLE);
        zos.setEncoding(CharEncoding.UTF_8);
        Collection<File> folderContent = FileUtils.listFiles(folderToZip, null, true);
        for (File file : folderContent) {
            String entryName = file.getPath().substring(folderToZip.getParent().length() + 1);
            entryName = FilenameUtils.separatorsToUnix(entryName);
            zos.putArchiveEntry(new ZipArchiveEntry(entryName));
            InputStream in = new FileInputStream(file);
            IOUtils.copy(in, zos);
            zos.closeArchiveEntry();
            IOUtils.closeQuietly(in);
        }
    } finally {
        if (zos != null) {
            IOUtils.closeQuietly(zos);
        }
    }
    return zipFile.length();
}

From source file:com.roamtouch.menuserver.utils.FileUtils.java

/**
 * removeExtention/*w  ww .  j  ava 2s .  c o  m*/
 * @param filePath
 * @return
 */
public static String removeExtention(String filePath) {
    File f = new File(filePath);
    if (f.isDirectory())
        return filePath;
    String name = f.getName();
    final int lastPeriodPos = name.lastIndexOf('.');
    if (lastPeriodPos <= 0) {
        return filePath;
    } else {
        File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));
        return renamed.getPath();
    }
}

From source file:com.discovery.darchrow.io.FileUtil.java

/**
 *  ??/*  w  w w .  java2s  .c  o m*/
 * 
 * <pre>
 * {@code
 *   Example 1:
 *      "mp2-product\\mp2-product-impl\\src\\main\\java\\com\\baozun\\mp2\\rpc\\impl\\item\\repo\\package-info.java"
 *      
 *       mp2-product
 * }
 * </pre>
 *
 * @param pathname
 *            ?????? File .??.
 * @return ,, E:/  E:/
 * @since 1.0.7
 */
public static final String getFileTopParentName(String pathname) {
    if (Validator.isNullOrEmpty(pathname)) {
        throw new NullPointerException("pathname can't be null/empty!");
    }

    File file = new File(pathname);

    String parent = file.getParent();

    if (Validator.isNullOrEmpty(parent)) {
        return pathname;
    }

    //
    String fileTopParentName = getFileTopParentName(file);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("pathname:[{}],fileTopParentName:[{}]", pathname, fileTopParentName);
    }
    return fileTopParentName;
}

From source file:net.ftb.minecraft.MCInstaller.java

public static void extractLegacy() {
    try {//w w w.  j  a  v  a 2s . c o m
        File f = new File(Settings.getSettings().getInstallPath() + File.separator + "libraries"
                + File.separator + "net.ftb.legacylaunch.FTBLegacyLaunch".replace(".", File.separator)
                + File.separator + "0.0.1" + File.separator + "FTBLegacyLaunch-0.0.1.jar");
        //Logger.logError("Extracting Legacy launch code to " + f.getAbsolutePath());
        if (!new File(f.getParent()).exists())
            new File(f.getParent()).mkdirs();
        if (f.exists())
            f.delete();//we want to have the current version always!!!
        URL u = LaunchFrame.class.getResource("/launch/FTBLegacyLaunch-0.0.1.jar");
        org.apache.commons.io.FileUtils.copyURLToFile(u, f);
    } catch (Exception e) {
        Logger.logError("Error extracting legacy launch to maven directory");
    }
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

public static File renameFile(File file, String newName) throws Exception {
    File newFile = new File(file.getParent(), newName);
    file.renameTo(newFile);//from  www  .  j ava2 s .c om
    return newFile;
}