Example usage for java.io File getPath

List of usage examples for java.io File getPath

Introduction

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

Prototype

public String getPath() 

Source Link

Document

Converts this abstract pathname into a pathname string.

Usage

From source file:com.textocat.textokit.commons.util.CorpusUtils.java

/**
 * @param baseDir//from www. j  av  a 2 s . com
 * @return function that returns the path of an arg File relative to the
 * given baseDir. It works only with the assumption that an arg File
 * is within baseDir. This is usually true to deal with a corpus
 * structure.
 */
public static Function<File, String> relativePathFunction(final File baseDir) {
    return new Function<File, String>() {
        @Override
        public String apply(File arg) {
            String argPath = arg.getPath();
            String basePath = baseDir.getPath();
            // FIXME test on different OS
            if (!basePath.endsWith(File.separator)) {
                // to avoid result being absolute path
                basePath += File.separator;
            }
            if (argPath.startsWith(basePath)) {
                return argPath.substring(basePath.length());
            } else {
                throw new IllegalArgumentException(String.format("File %s is not in dir %s", arg, baseDir));
            }
        }
    };
}

From source file:org.opennms.ng.services.capsdconfig.CapsdConfigFactory.java

/**
 * Load the config from the default config file and create the singleton
 * instance of this factory.//from   w  ww. j a  va 2  s.  c om
 *
 * @exception java.io.IOException
 *                Thrown if the specified config file cannot be read
 * @exception org.exolab.castor.xml.MarshalException
 *                Thrown if the file does not conform to the schema.
 * @exception org.exolab.castor.xml.ValidationException
 *                Thrown if the contents do not match the required schema.
 * @throws java.io.IOException if any.
 * @throws org.exolab.castor.xml.MarshalException if any.
 * @throws org.exolab.castor.xml.ValidationException if any.
 */
public static synchronized void init() throws IOException, MarshalException, ValidationException {
    if (isLoaded()) {
        /*
         * init already called - return
         * to reload, reload() will need to be called
         */
        return;
    }

    File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.CAPSD_CONFIG_FILE_NAME);

    LOG.debug("init: config file path: {}", cfgFile.getPath());

    DefaultCapsdConfigManager capsdConfig = new DefaultCapsdConfigManager();
    capsdConfig.update();
    setInstance(capsdConfig);
}

From source file:extractjavadoc.TraversalFiles.java

public static void fileList(File inputFile, int node, ArrayList<String> path, String folderPath,
        boolean ifGeneral, Map<String, Boolean> libraryTypeCondition) {
    node++;/*w  w  w.  jav  a2s  .c o  m*/
    File[] files = inputFile.listFiles();
    if (!inputFile.exists()) {
        System.out.println("File doesn't exist!");
    } else if (inputFile.isDirectory()) {
        path.add(inputFile.getName());

        for (File f : files) {
            for (int i = 0; i < node - 1; i++) {
                System.out.print(" ");
            }
            System.out.print("|-" + f.getPath());

            String ext = FilenameUtils.getExtension(f.getName());
            if (ext.equals("html")) {
                try {
                    System.out.println(" => extracted");

                    //Get extracted file location and add it to output file name,
                    //in order to avoid files in different folder 
                    //have the same name.
                    String fileLocation = "";
                    for (String tmpPath : path) {
                        fileLocation += "-" + tmpPath;
                    }

                    String usefulJavadocFilePath = folderPath + "/" + "javadoc-" + f.getName() + fileLocation
                            + ".txt";
                    //                        String usefulJavadocFilePath = folderPath + "/" + "javadoc-" + f.getName() + ".txt";

                    //create output file for usefuljavadoc
                    File usefulJavadocFile = new File(usefulJavadocFilePath);
                    if (usefulJavadocFile.createNewFile()) {
                        System.out.println("Create successful: " + usefulJavadocFile.getName());
                    }

                    //extract useful javadoc
                    ExtractHTMLContent extractJavadoc = new ExtractHTMLContent(f, usefulJavadocFile, ifGeneral,
                            libraryTypeCondition);
                    extractJavadoc.extractHTMLContent();
                } catch (IOException ex) {
                    Logger.getLogger(TraversalFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                //                   System.out.println(" isn't a java file or html file.");
                System.out.println(" isn't a html file.");
            }
            fileList(f, node, path, folderPath, ifGeneral, libraryTypeCondition);
        }
        path.remove(node - 1);
    }
}

From source file:com.pieframework.runtime.utils.ArtifactManager.java

private static String getCommonPathRoot(List<File> flist) {

    String result = "";
    int shortestPathSize = 0;
    Map<String, File> commonPath = new HashMap<String, File>();

    for (File f : flist) {

        String[] tmp = f.getPath().split("\\\\");
        if (tmp.length == 0) {
            tmp = f.getPath().split("/");
        }// w  w  w. j a v a  2  s . c o  m

        if (shortestPathSize == 0) {
            shortestPathSize = tmp.length;
        }

        if (tmp.length <= shortestPathSize) {
            shortestPathSize = tmp.length;
            commonPath.put(f.getPath(), f);
            result = f.getPath();
        }

    }

    if (commonPath.size() > 1) {
        throw new RuntimeException("There is more than one root directory provided in file list.");
    }

    return result;
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static File unzipFile(File zipFile, File file) {
    System.out.println("path to zipFile: " + zipFile.getPath());
    System.out.println("file to extract: " + file.getPath());
    String fileName = null;//from w w w . j  a v  a 2  s.  c om

    try {
        zipFile.mkdir();
        BufferedOutputStream dest = null;
        BufferedInputStream is = null;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            // System.out.println(entry.getName());
            if (entry.getName().substring(entry.getName().indexOf("/") + 1).equals(file.getName())) {
                //   if (entry.getName().contains(file.getName())){
                System.out.println("firmware to extract found.");

                String tempFolder = System.getProperty("user.dir") + File.separatorChar + "tmp"
                        + File.separatorChar;
                if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                    tempFolder = System.getProperty("user.home")
                            + "/Library/Preferences/kkMulticopterFlashTool/";
                }

                String newDir;
                if (entry.getName().indexOf("/") == -1) {
                    newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
                } else {
                    newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
                }
                String folder = tempFolder + newDir;
                System.out.println("Create folder: " + folder);
                if ((new File(folder).mkdir())) {
                    System.out.println("Done.");
                    ;
                }

                System.out.println("Extracting: " + entry);
                is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                fileName = tempFolder + entry.getName();
                FileOutputStream fos = new FileOutputStream(tempFolder + entry.getName());
                dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                break;
            }
        }
    } catch (ZipException e) {
        zipFile.delete();
        kk.err(Translatrix._("error.zipfileDamaged"));
        kk.err(Translatrix._("reportProblem"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new File(fileName);
}

From source file:net.estinet.gFeatures.Feature.gRanks.gRanks.java

public static List<String> getPermsFile(File f) throws IOException {
    List<String> perms = Files.readAllLines(Paths.get(f.getPath()), StandardCharsets.UTF_8);
    for (int i = 0; i < perms.size(); i++) {
        perms.set(i, perms.get(i).replace("\r", ""));
        if (perms.get(i).equals("")) {
            if (i != 0)
                i--;//from  w w  w. j a va 2 s.  com
            perms.remove(i);
        }
    }
    return perms;
}

From source file:com.ctriposs.rest4j.server.util.FileClassNameScanner.java

/**
 * Construct map from fully qualified class name to filename whose sources are found under a given source directory.
 * All source files are required to have an extension.
 *
 * @param sourceDir the source directory to scan
 * @param requiredExtension only include files whose extension equals to this parameter
 *                          null if no specific extension is required
 * @return map from fully qualified class name to filename for scanned source files.
 *//* w  ww . j  a  v a 2 s.  c  o  m*/
public static Map<String, String> scan(String sourceDir, String requiredExtension) {
    final String sourceDirWithSeparator = sourceDir.endsWith(File.separator) ? sourceDir
            : sourceDir + File.separator;
    final File dir = new File(sourceDirWithSeparator);
    if (!dir.exists() || !dir.isDirectory()) {
        return Collections.emptyMap();
    }

    // suppress the warning because of inconsistent FileUtils interface
    @SuppressWarnings("unchecked")
    final Collection<File> files = (Collection<File>) FileUtils.listFiles(dir, null, true);
    final Map<String, String> classFileNames = new HashMap<String, String>();
    final int prefixLength = sourceDirWithSeparator.length();
    for (File f : files) {
        assert (f.exists() && f.isFile());

        final int extensionIndex = f.getName().lastIndexOf('.');
        final String filePath = f.getPath();
        if (extensionIndex < 0 || !filePath.startsWith(sourceDirWithSeparator)) {
            continue;
        }

        final int reverseExtensionIndex = f.getName().length() - extensionIndex;
        final String classPathName = filePath.substring(prefixLength,
                filePath.length() - reverseExtensionIndex);
        if (classPathName.contains(".")) {
            // dot is not allowed in package name, thus not allowed in the directory path
            continue;
        }

        if (requiredExtension != null) {
            final String extension = f.getName().substring(extensionIndex + 1);
            if (!extension.equals(requiredExtension)) {
                continue;
            }
        }
        classFileNames.put(classPathName.replace(File.separator, "."), filePath);
    }

    return classFileNames;
}

From source file:Main.java

public static String getAppCachePath(Context context) {
    String cachePath;//from ww  w. ja  v a 2s  .  com
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        File externalCacheDir = context.getExternalCacheDir();
        // context.getExternalCacheDir() maybe null
        if (externalCacheDir == null) {
            cachePath = context.getCacheDir().getPath();
        } else {
            cachePath = externalCacheDir.getPath();
        }
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return cachePath;
}

From source file:Main.java

private static float getSize(String path, Float size) {
    File file = new File(path);
    if (file.exists()) {
        if (file.isDirectory()) {
            String[] children = file.list();
            for (int fileIndex = 0; fileIndex < children.length; ++fileIndex) {
                float tmpSize = getSize(file.getPath() + File.separator + children[fileIndex], size) / 1000;
                size += tmpSize;//from  w  w  w. j a va  2  s  .  c o  m
            }
        } else if (file.isFile()) {
            size += file.length();
        }
    }
    return size;
}

From source file:org.camunda.bpm.cockpit.test.util.DeploymentHelper.java

public static void addFiles(JavaArchive archive, String prefix, File dir) {
    if (!dir.isDirectory()) {
        throw new IllegalArgumentException("not a directory");
    }//  www . j av  a 2s.c o  m

    addFiles(archive, prefix, dir.getPath(), dir);
}