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:ips1ap101.lib.core.db.util.Reporter.java

private static String getParent(File file, Long usuario) {
    String sep = System.getProperties().getProperty("file.separator");
    String path = file.getPath();
    String substring = sep + "user-defined" + sep + Utils.getUserDir(usuario) + sep;
    return StringUtils.contains(path, substring) ? file.getParentFile().getParent() : file.getParent();
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ??dir//from ww  w.  j ava2s.  c om
 *
 * @param permission
 *            ??
 * @param filePath
 *            ??
 * @param dir
 *            ????(?filePath"/"??)
 */
public static void setPermissionUntilDir(String permission, String filePath, String dir) {
    String dirPath = new File(dir).getAbsolutePath();
    if (null == filePath || !filePath.startsWith(dirPath)) {
        return;
    }
    File fileObj = new File(filePath);
    while (!dirPath.equals(fileObj.getAbsolutePath())) {
        String path = fileObj.getAbsolutePath();
        // ??
        setPermission(permission, path);
        fileObj = new File(fileObj.getParent());
    }
}

From source file:com.att.aro.core.util.Util.java

/**
 * will return the full path of dir where ARO.jar is running from.
 * //from   w w w  . j a v  a2s  .  co m
 * @return full path of directory
 */
public static String getCurrentRunningDir() {
    String dir = "";
    File filepath = new File(Util.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    dir = filepath.getParent();
    return dir;
}

From source file:com.ieasy.basic.util.file.FileUtils.java

/**
 * ??/*from   ww  w  .j a v a  2 s  .  com*/
 * 
 * @param filePath
 *            ??
 * @param definedName
 *            ??
 * @return
 */
public static boolean rename(String filePath, String definedName) {
    File file = new File(filePath);
    if (file.exists()) {
        String fileExt = file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase();

        String rename = file.getParent() + "/" + definedName + "." + fileExt;

        if (file.renameTo(new File(rename))) {
            logger.debug("???{} TO {}", file.getAbsoluteFile(), rename);
            return true;
        } else {
            logger.debug("??{} TO {}", file.getAbsoluteFile(),
                    rename);
            return false;
        }
    } else {
        logger.debug("?{}", file.getAbsoluteFile());
        return false;
    }
}

From source file:br.com.manish.ahy.kernel.util.FileUtil.java

public static void copyFile(String from, String to, Boolean overwrite) {

    try {/*w  ww  .jav  a  2  s . co  m*/
        File fromFile = new File(from);
        File toFile = new File(to);

        if (!fromFile.exists()) {
            throw new IOException("File not found: " + from);
        }
        if (!fromFile.isFile()) {
            throw new IOException("Can't copy directories: " + from);
        }
        if (!fromFile.canRead()) {
            throw new IOException("Can't read file: " + from);
        }

        if (toFile.isDirectory()) {
            toFile = new File(toFile, fromFile.getName());
        }

        if (toFile.exists() && !overwrite) {
            throw new IOException("File already exists.");
        } else {
            String parent = toFile.getParent();
            if (parent == null) {
                parent = System.getProperty("user.dir");
            }
            File dir = new File(parent);
            if (!dir.exists()) {
                throw new IOException("Destination directory does not exist: " + parent);
            }
            if (dir.isFile()) {
                throw new IOException("Destination is not a valid directory: " + parent);
            }
            if (!dir.canWrite()) {
                throw new IOException("Can't write on destination: " + parent);
            }
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {

            fis = new FileInputStream(fromFile);
            fos = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

        } finally {
            if (from != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
            if (to != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error(e);
                }
            }
        }

    } catch (Exception e) {
        throw new OopsException(e, "Problems when copying file.");
    }
}

From source file:edu.harvard.mcz.imagecapture.ImageCaptureProperties.java

/** 
 * Given a file, is that file inside the path described by ImageCaptureProperties.KEY_IMAGEBASE
 * /*from w ww  .  j a va  2  s .c o  m*/
 * @param aFile
 * @return true if aFile is inside imagebase, false otherwise.
 */
public static boolean isInPathBelowBase(File aFile) {
    boolean result = false;
    String base = Singleton.getSingletonInstance().getProperties().getProperties()
            .getProperty(ImageCaptureProperties.KEY_IMAGEBASE);
    String filePath = aFile.getPath();

    if (aFile.isFile()) {
        filePath = aFile.getParent();
    }
    log.debug("Provided path to test: " + filePath);
    if (File.separator.equals("\\")) {
        if (!base.endsWith("\\")) {
            base = base + "\\";
        }
        // the separator "\" is represented in java as "\\" and in a java regular expression as "\\\\"
        base = base.replaceAll("\\\\", "\\\\\\\\");
        filePath = filePath.replaceAll("\\\\", "\\\\\\\\");
    } else {
        if (!base.endsWith("/")) {
            base = base + "/";
        }
        if (!filePath.endsWith("/")) {
            filePath = filePath + "/";
        }
    }
    log.debug("Base path for test: " + base);
    if (filePath.startsWith(base)) {
        result = true;
    }
    return result;
}

From source file:com.docd.purefm.test.CommandLineFileTest.java

private static void testAgainstJavaIoFile(final CommandLineFile genericFile, final File javaFile,
        final boolean testDate) throws Throwable {
    assertEquals(javaFile, genericFile.toFile());
    assertEquals(javaFile.getName(), genericFile.getName());
    assertEquals(javaFile.getAbsolutePath(), genericFile.getAbsolutePath());
    assertEquals(javaFile.exists(), genericFile.exists());
    assertEquals(javaFile.canRead(), genericFile.canRead());
    assertEquals(javaFile.canWrite(), genericFile.canWrite());
    assertEquals(javaFile.canExecute(), genericFile.canExecute());
    assertEquals(javaFile.getPath(), genericFile.getPath());
    assertEquals(javaFile.getParent(), genericFile.getParent());
    final File parentFile;
    final GenericFile genericParentFile = genericFile.getParentFile();
    if (genericParentFile == null) {
        parentFile = null;/*  w  ww . ja  v  a 2 s . c  o  m*/
    } else {
        parentFile = genericParentFile.toFile();
    }
    assertEquals(javaFile.getParentFile(), parentFile);
    assertEquals(javaFile.length(), genericFile.length());
    try {
        assertEquals(FileUtils.isSymlink(javaFile), genericFile.isSymlink());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        assertEquals(javaFile.getCanonicalPath(), genericFile.getCanonicalPath());
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertEquals(javaFile.length(), genericFile.length());
    assertEquals(javaFile.isDirectory(), genericFile.isDirectory());
    if (genericFile.isDirectory()) {
        assertTrue(listedPathsEqual(javaFile.list(), genericFile.list()));
        assertTrue(listedFilesEqual(javaFile.listFiles(), genericFile.listFiles()));
    }

    if (testDate) {
        assertEquals(PFMTextUtils.humanReadableDate(javaFile.lastModified(), false),
                PFMTextUtils.humanReadableDate(genericFile.lastModified(), true));
    }
}

From source file:graphene.util.fs.FileUtils.java

public static boolean renameFile(final File srcFile, final File renameToFile) {
        if (!srcFile.exists()) {
            throw new NotFoundException("Source file[" + srcFile.getName() + "] not found");
        }/* w w w  .ja v  a2  s  .  c  om*/
        if (renameToFile.exists()) {
            throw new NotFoundException("Target file[" + renameToFile.getName() + "] already exists");
        }
        if (!renameToFile.getParentFile().isDirectory()) {
            throw new NotFoundException("Target directory[" + renameToFile.getParent() + "] does not exists");
        }
        int count = 0;
        boolean renamed;
        do {
            renamed = srcFile.renameTo(renameToFile);
            if (!renamed) {
                count++;
                waitSome();
            }
        } while (!renamed && (count <= WINDOWS_RETRY_COUNT));
        return renamed;
    }

From source file:com.ephesoft.dcma.util.PDFUtil.java

/**
 * The <code>getSelectedPdfFile</code> method is used to limit the file
 * to the page limit given.// ww w. j  ava  2s  . co  m
 * 
 * @param pdfFile {@link File} pdf file from which limit has to be applied
 * @param pageLimit int
 * @throws IOException if file is not found
 * @throws DocumentException if document cannot be created
 */
public static void getSelectedPdfFile(final File pdfFile, final int pageLimit)
        throws IOException, DocumentException {
    PdfReader reader = null;
    Document document = null;
    PdfContentByte contentByte = null;
    PdfWriter writer = null;
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    File newFile = null;
    if (null != pdfFile && pdfFile.exists()) {
        try {
            document = new Document();
            fileInputStream = new FileInputStream(pdfFile);

            String name = pdfFile.getName();
            final int indexOf = name.lastIndexOf(IUtilCommonConstants.DOT);
            name = name.substring(0, indexOf);
            final String finalPath = pdfFile.getParent() + File.separator + name + System.currentTimeMillis()
                    + IUtilCommonConstants.EXTENSION_PDF;
            newFile = new File(finalPath);
            fileOutputStream = new FileOutputStream(finalPath);
            writer = PdfWriter.getInstance(document, fileOutputStream);
            document.open();
            contentByte = writer.getDirectContent();

            reader = new PdfReader(fileInputStream);
            for (int i = 1; i <= pageLimit; i++) {
                document.newPage();

                // import the page from source pdf
                final PdfImportedPage page = writer.getImportedPage(reader, i);

                // add the page to the destination pdf
                contentByte.addTemplate(page, 0, 0);
                page.closePath();
            }
        } finally {
            closePassedStream(reader, document, contentByte, writer, fileInputStream, fileOutputStream);
        }
        if (pdfFile.delete() && null != newFile) {
            newFile.renameTo(pdfFile);
        } else {
            if (null != newFile) {
                newFile.delete();
            }
        }
    }
}

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

/**
 * // w  ww .  j  a  v a 2s. co  m
 * @param ecoreProject
 * @param busProject
 * @param templateProject
 */
@SuppressWarnings("unchecked")
public static void configureRestTemplate(final File ecoreProject, final File busProject,
        final File templateProject) {
    File manifestFile;
    String configuration;
    String exportPackage;
    String bundleActivator;
    String templateContent;
    String oxmConfiguration;

    try {
        // Copy lib
        FileUtils.copyDirectory(new File(templateProject, "/lib.rest"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit/lib"));
        FileUtils.copyDirectory(new File(templateProject, "/lib.rest"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/lib"));
        FileUtils.copyDirectory(new File(templateProject, "/lib.jdom"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/lib"));

        // Add rest configuration
        templateContent = FileUtils.readFileToString(new File(templateProject, "/templates/beans.xml"));
        configuration = FileUtils.readFileToString(new File(busProject,
                "/src/main/resources/META-INF/spring/applicationContext-contentresolver.xml"));
        oxmConfiguration = configuration.substring(configuration.indexOf("<oxm:"),
                configuration.indexOf("</oxm:jaxb2-marshaller>") + "</oxm:jaxb2-marshaller>".length());
        templateContent = templateContent.replace("<!-- oxm -->", oxmConfiguration);
        FileUtils.writeStringToFile(
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit/beans.xml"),
                templateContent);
        FileUtils.writeStringToFile(
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/beans.xml"),
                templateContent);

        // Copy imb types
        FileUtils.copyDirectory(new File(busProject, "/src/main/java/imb"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit/src/imb"));
        FileUtils.copyDirectory(new File(busProject, "/src/main/java/imb"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/src/imb"));

        // Update classpath
        FileUtils.copyFile(new File(templateProject, "templates/classpath.xml"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit/.classpath"));
        FileUtils.copyFile(new File(templateProject, "templates/classpath.editor.xml"),
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/.classpath"));
        templateContent = FileUtils.readFileToString(new File(templateProject, "/templates/project.xml"));
        FileUtils.writeStringToFile(
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".edit/.project"),
                templateContent.replace("${projectName}", ecoreProject.getName()));
        FileUtils.writeStringToFile(
                new File(ecoreProject.getParent(), ecoreProject.getName() + ".editor/.project"),
                templateContent.replace("${projectName}", ecoreProject.getName()));

        // Update Manifest
        bundleActivator = null;
        exportPackage = null;

        // edit project
        manifestFile = new File(ecoreProject.getParent(),
                ecoreProject.getName() + ".edit/META-INF/MANIFEST.MF");
        for (String line : (List<String>) FileUtils.readLines(manifestFile)) {
            if (line.startsWith("Bundle-Activator")) {
                bundleActivator = line.replace("Bundle-Activator: ", "");
            } else if (line.startsWith("Export-Package: ")) {
                exportPackage = line.replace("Export-Package: ", "");
            }
        }
        templateContent = FileUtils.readFileToString(new File(templateProject, "/templates/manifest.MF"));
        templateContent = templateContent.replaceAll("\\$\\{projectName\\}", ecoreProject.getName());
        templateContent = templateContent.replace("${bundleActivator}", bundleActivator);
        templateContent = templateContent.replace("${exportPackage}", exportPackage);
        FileUtils.writeStringToFile(manifestFile, templateContent);

        // editor project
        manifestFile = new File(ecoreProject.getParent(),
                ecoreProject.getName() + ".editor/META-INF/MANIFEST.MF");
        for (String line : (List<String>) FileUtils.readLines(manifestFile)) {
            if (line.startsWith("Bundle-Activator")) {
                bundleActivator = line.replace("Bundle-Activator: ", "");
            } else if (line.startsWith("Export-Package: ")) {
                exportPackage = line.replace("Export-Package: ", "");
            }
        }
        templateContent = FileUtils
                .readFileToString(new File(templateProject, "/templates/manifest.editor.MF"));
        templateContent = templateContent.replaceAll("\\$\\{projectName\\}", ecoreProject.getName());
        templateContent = templateContent.replace("${bundleActivator}", bundleActivator);
        templateContent = templateContent.replace("${exportPackage}", exportPackage);
        FileUtils.writeStringToFile(manifestFile, templateContent);

    } catch (Exception e) {
        System.out.println("Error while configuring Rest template: " + e.getMessage());
    }
}