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.alibaba.ims.platform.util.ExcelUtil.java

private static Workbook createWorkbook(File file) {
    try {/* ww  w. j ava  2 s.  c om*/
        return WorkbookFactory.create(file);
    } catch (Exception e) {
        logger.error("Read workbook from file error, file:" + file.getPath(), e);
    }
    return null;
}

From source file:com.phonegap.DirectoryManager.java

/**
 * Get the free disk space on the SD card
 * // w  w  w  . jav a2 s.c  om
 * @return       Size in KB or -1 if not available
 */
protected static long getFreeDiskSpace() {
    String status = Environment.getExternalStorageState();
    long freeSpace = 0;

    // If SD card exists
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        try {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            freeSpace = availableBlocks * blockSize / 1024;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // If no SD card, then return -1
    else {
        return -1;
    }

    return (freeSpace);
}

From source file:com.bfd.harpc.common.configure.PathUtils.java

/**
 * ???classpathjarjar/*from  w ww.  j  ava2s .  c o  m*/
 * <p>
 * 
 * @return
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
public static String getAppDir(Class<?> clazz) {
    File f;
    try {
        f = new File(getCodeLocation(clazz).toURI().getPath());
        return f.isFile() ? f.getParent() : f.getPath();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.tc.utils.DxlUtils.java

private static void write(File dir, String fileName, byte[] byteMe) {
    OutputStream out = null;//from   w  w  w. j a  v a2s  .  c  o  m
    try {
        out = new FileOutputStream(dir.getPath() + IOUtils.DIR_SEPARATOR + fileName);
        IOUtils.write(byteMe, out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
    }

}

From source file:com.fujitsu.dc.test.jersey.bar.BarInstallTestUtils.java

/**
 * bar?.//from  w  ww . j ava2s  .  c o  m
 * @param barFile bar?
 * @return ??bar
 */
public static byte[] readBarFile(File barFile) {
    InputStream is = ClassLoader.getSystemResourceAsStream(barFile.getPath());
    ByteBuffer buff = ByteBuffer.allocate(READ_BUFFER_SIZE);
    log.debug(String.valueOf(buff.capacity()));
    try {
        byte[] bbuf = new byte[SIZE_KB];
        int size;
        while ((size = is.read(bbuf)) != -1) {
            buff.put(bbuf, 0, size);
        }
    } catch (IOException e) {
        throw new RuntimeException("failed to load bar file:" + barFile.getPath(), e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            throw new RuntimeException("failed to close bar file:" + barFile.getPath(), e);
        }
    }
    int size = buff.position();
    buff.flip();
    byte[] retv = new byte[size];
    buff.get(retv, 0, size);
    return retv;
}

From source file:com.pieframework.runtime.core.Installer.java

public static void run(String inputs) {
    //Generate configuration file if one does not exists and overwrite is not specified
    Properties prop = generateProps(inputs);

    System.out.println("Running installer for pie for " + Configuration.getOs());
    //Based on config create directories
    List<File> dirs = new ArrayList<File>();

    dirs.add(new File(prop.getProperty("pie.home")));
    dirs.add(new File(prop.getProperty("pie.lib")));
    dirs.add(new File(prop.getProperty("pie.tmp")));
    dirs.add(new File(prop.getProperty("pie.data")));
    dirs.add(new File(prop.getProperty("pie.ext")));
    dirs.add(new File(new File(prop.getProperty("pie.bootstrap")).getParent()));
    dirs.add(new File(new File(prop.getProperty("pie.log")).getParent()));

    for (File f : dirs) {
        //System.out.println(f.getPath());
        f.mkdirs();//from ww  w  .  ja va 2s .  c om
    }

    //Create the pie config file and insert all the properties we created
    createConfigFile(prop);

    //Copy the current pie.jar in the right place according to config
    File currentPieJar = new File(getJarPath());
    File newPieJar = new File(prop.getProperty("pie.jar"));

    try {
        if (!newPieJar.getPath().equalsIgnoreCase(currentPieJar.getPath())) {
            FileUtils.copyFile(currentPieJar, newPieJar);
        }

        if (newPieJar.exists() && newPieJar.isFile()) {
            System.out.println("info: created exe file " + newPieJar.getPath());
        } else {
            throw new RuntimeException("error: failed creating file " + newPieJar.getPath());
        }
    } catch (IOException e) {
        throw new RuntimeException("error: failed creating file " + newPieJar.getPath(), e);
    }
}

From source file:com.alvermont.terraj.fracplanet.io.JarLibraryLoader.java

/**
 * Loads a native library. The native library is assumed
 * to be in a platform specific subdirectory of the lib directory in the jar. 
 * The library is writen to a local temporary directory and (hopefully) 
 * deleted at JVM exit./*from  w  ww .jav  a 2s  .  c o m*/
 *
 * Dll extraction code taken from Gabriele Piero Nizzoli
 * http://www.nizzoli.net/index.php?itemid=15
 *
 * @param name The base name of the library to be loaded
 * @throws IOException if there is a problem loading the library
 */
public static void loadLibrary(String name) throws IOException {
    try {
        File libFile = extractLibrary(name);

        if (libFile != null) {
            log.debug("Calling System.load on: " + libFile.getPath());

            System.load(libFile.getPath());
        }
    } catch (IOException ioe) {
        log.error("IOException loading library", ioe);

        throw ioe;
    }
}

From source file:com.fjn.helper.common.io.file.common.FileUtil.java

/**
 * ?//from  w  ww. ja va2 s  . com
 * @param dir
 * @return
 */
public static File ensureDirExists(File dir) {
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            throw new FileDirFaultException(dir.getPath());
        }
    }
    return dir;
}

From source file:net.adamcin.oakpal.testing.TestPackageUtil.java

private static void add(final File root, final File source, final JarOutputStream target) throws IOException {
    if (root == null || source == null) {
        throw new IllegalArgumentException("Cannot add from a null file");
    }//from   w w  w  . j  av a2  s  .c om
    if (!(source.getPath() + "/").startsWith(root.getPath() + "/")) {
        throw new IllegalArgumentException("source must be the same file or a child of root");
    }
    final String relPath;
    if (!root.getPath().equals(source.getPath())) {
        relPath = source.getPath().substring(root.getPath().length() + 1).replace(File.separator, "/");
    } else {
        relPath = "";
    }
    if (source.isDirectory()) {
        if (!relPath.isEmpty()) {
            String name = relPath;
            if (!name.endsWith("/")) {
                name += "/";
            }
            JarEntry entry = new JarEntry(name);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();
        }
        File[] children = source.listFiles();
        if (children != null) {
            for (File nestedFile : children) {
                add(root, nestedFile, target);
            }
        }
    } else {
        JarEntry entry = new JarEntry(relPath);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        try (InputStream in = new BufferedInputStream(new FileInputStream(source))) {
            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        }
    }
}

From source file:net.sf.taverna.t2.maven.plugins.Utils.java

public static void downloadFile(String resourceName, File file, Wagon wagon, Log log)
        throws MojoExecutionException, ResourceDoesNotExistException {
    String resourceUrl = getResourceUrl(wagon, resourceName);
    File digestFile = new File(file.getPath() + ".md5");
    try {/*from w w  w  .j  a  v a  2  s .  c om*/
        log.info(String.format("Downloading %1$s to %2$s", resourceUrl, file));
        wagon.get(resourceName, file);
        wagon.get(resourceName + ".md5", digestFile);
    } catch (TransferFailedException e) {
        throw new MojoExecutionException(String.format("Error transferring %1$s to %2$s", resourceUrl, file),
                e);
    } catch (AuthorizationException e) {
        throw new MojoExecutionException(
                String.format("Authentication error transferring %1$s to %2$s", resourceUrl, file), e);
    }
    try {
        String digestString1 = DigestUtils.md5Hex(new FileInputStream(file));
        String digestString2 = FileUtils.readFileToString(digestFile);
        if (!digestString1.equals(digestString2)) {
            throw new MojoExecutionException(
                    String.format("Error downloading file: digsests not equal. (%1$s != %2$s)", digestString1,
                            digestString2));
        }
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Error checking digest for %1$s", file), e);
    }
}