Example usage for java.io File length

List of usage examples for java.io File length

Introduction

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

Prototype

public long length() 

Source Link

Document

Returns the length of the file denoted by this abstract pathname.

Usage

From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java

/**
 * @param infileName//  ww  w . java 2  s  .c  o m
 * @param outFileName
 * @param changeListener
 * @return
 */
public static boolean uncompressFile(final String infileName, final String outFileName) {
    File file = new File(infileName);
    if (file.exists()) {
        long fileSize = file.length();
        if (fileSize > 0) {
            try {
                GZIPInputStream fis = new GZIPInputStream(new FileInputStream(infileName));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFileName));

                byte[] bytes = new byte[BUFFER_SIZE * 10];

                while (true) {
                    int len = fis.read(bytes);
                    if (len > 0) {
                        fos.write(bytes, 0, len);
                    } else {
                        break;
                    }
                }

                fis.close();
                fos.close();

                return true;

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            // file doesn't exist
        }
    } else {
        // file doesn't exist
    }
    return false;
}

From source file:com.codercowboy.photo.organizer.service.PhotoIndexer.java

public static Photo indexFile(File f) throws Exception {
    if (!f.exists()) {
        throw new IOException("Cannot index file, file does not exist: " + f.getAbsolutePath());
    }//from ww w .j  a v a 2 s  .c  o m
    if (!f.canRead()) {
        throw new IOException(
                "Cannot index file, file is not readable, permission denied:" + f.getAbsolutePath());
    }

    Photo p = new Photo();
    p.setName(f.getName());
    //TODO: this should be relative path to parent
    p.setFileRelativePath(f.getAbsolutePath());
    p.setFileSize(f.length());
    FileInputStream fis = new FileInputStream(f);
    try {
        p.setMd5Checksum(DigestUtils.md5Hex(fis));
    } finally {
        fis.close();
    }
    return p;
}

From source file:net.pms.util.OpenSubtitle.java

public static String[] getInfo(File f, String formattedName, RendererConfiguration r) throws IOException {
    String[] res = getInfo(getHash(f), f.length(), null, null, r);
    if (res == null || res.length == 0) { // no good on hash! try imdb
        String imdb = ImdbUtil.extractImdb(f);
        if (StringUtil.hasValue(imdb)) {
            res = getInfo(null, 0, imdb, null, r);
        }// w w  w  .j ava 2s  . com
    }
    if (res == null || res.length == 0) { // final try, use the name
        if (StringUtils.isNotEmpty(formattedName)) {
            res = getInfo(null, 0, null, formattedName, r);
        } else {
            res = getInfo(null, 0, null, f.getName(), r);
        }
    }
    return res;
}

From source file:cn.fql.utility.FileUtility.java

/**
 * Copy source folder's content to a new folder, if there are existed duplicated files and overwrite
 * flag is true, it might overwrite it/*from   www  .  ja va2 s .c o  m*/
 *
 * @param sourceFolder   source folder path
 * @param newFolder      new folder path
 * @param forceOverwrite determine whether it is required to overwrite for duplicated files
 */
public static void copyFolder(String sourceFolder, String newFolder, boolean forceOverwrite) {
    String oldPath = StringUtils.replace(sourceFolder, "/", File.separator);
    String newPath = StringUtils.replace(newFolder, "/", File.separator);
    if (!oldPath.endsWith(File.separator)) {
        oldPath = oldPath + File.separator;
    }
    if (!newPath.endsWith(File.separator)) {
        newPath = newPath + File.separator;
    }
    new File(newPath).mkdirs();
    File sourceDir = new File(oldPath);
    String[] files = sourceDir.list();
    File oldFile;
    File newFile;
    for (int i = 0; i < files.length; i++) {
        oldFile = new File(oldPath + files[i]);
        newFile = new File(newPath + files[i]);
        if (oldFile.isFile() && (!forceOverwrite || (!newFile.exists() || oldFile.length() != newFile.length()
                || oldFile.lastModified() > newFile.lastModified()))) {
            copyFile(oldFile.getPath(), newFile.getPath());
        }
        if (oldFile.isDirectory()) {
            copyFolder(oldPath + files[i], newPath + files[i], forceOverwrite);
        }
    }
}

From source file:com.comcast.cats.service.util.VideoRecorderUtil.java

/**
 * Return file size in bytes.//  w w w.j  a  va 2  s .c  om
 * 
 * @param filePath
 * @return
 */
public static synchronized double getFileSize(String filePath) {
    double sizeInBytes = 0L;

    File file = new File(filePath);

    if (!file.exists() || !file.isFile()) {
        sizeInBytes = -1;
    } else {
        sizeInBytes = file.length();
    }

    return sizeInBytes;
}

From source file:com.zimbra.cs.service.util.ItemDataFile.java

static void addFile(File f, String topdir, Set<MailItem.Type> types, TarOutputStream tos) throws IOException {
    ItemData id = null;/*from w w w.  j  a  va2s.  co  m*/
    String path = f.getPath();
    File mf = new File(path + ".meta");
    TarEntry te;
    MailItem.Type type;

    if (path.indexOf(topdir) == 0)
        path = path.substring(topdir.length() + 1);
    path = path.replace('\\', '/');
    if (mf.exists()) {
        byte[] meta = new byte[(int) mf.length()];
        FileInputStream fis = new FileInputStream(mf);

        if (fis.read(meta) != mf.length()) {
            throw new IOException("meta read err: " + f.getPath());
        }
        fis.close();
        id = new ItemData(meta);
        type = MailItem.Type.of(id.ud.type);
        if (skip(types, type)) {
            return;
        }
        te = new TarEntry(path + ".meta");
        System.out.println(te.getName());
        te.setGroupName(MailItem.Type.of(id.ud.type).toString());
        te.setMajorDeviceId(id.ud.type);
        te.setModTime(mf.lastModified());
        te.setSize(meta.length);
        tos.putNextEntry(te);
        tos.write(meta);
        tos.closeEntry();
    } else {
        if (path.endsWith(".csv") || path.endsWith(".vcf")) {
            type = MailItem.Type.CONTACT;
        } else if (path.endsWith(".eml")) {
            type = MailItem.Type.MESSAGE;
        } else if (path.endsWith(".ics")) {
            if (path.startsWith("Tasks/")) {
                type = MailItem.Type.TASK;
            } else {
                type = MailItem.Type.APPOINTMENT;
            }
        } else if (path.endsWith(".wiki")) {
            type = MailItem.Type.WIKI;
        } else {
            type = MailItem.Type.DOCUMENT;
        }
        if (skip(types, type)) {

        }
        return;
    }
    if (f.exists() && !f.isDirectory() && (id != null || types == null)) {
        byte[] buf = new byte[TarBuffer.DEFAULT_BLKSIZE];
        FileInputStream fis = new FileInputStream(f);
        int in;

        te = new TarEntry(path);
        System.out.println(te.getName());
        te.setGroupName(MailItem.Type.of(id.ud.type).toString());
        te.setMajorDeviceId(id.ud.type);
        te.setModTime(mf.lastModified());
        te.setSize(f.length());
        tos.putNextEntry(te);
        while ((in = fis.read(buf)) > 0)
            tos.write(buf, 0, in);
        fis.close();
        tos.closeEntry();
    }
}

From source file:net.sf.gazpachoquest.rest.auth.TokenStore.java

/**
 * Creates a byte array of entry from the current state of the system:
 * <ul>/*from w  w w .j  a  v  a2  s.co m*/
 * <li>The current system time in milliseconds since the epoch</li>
 * <li>The number of nanoseconds since system startup</li>
 * <li>The name, size and last modification time of the files in the
 * <code>java.io.tmpdir</code> folder.</li>
 * </ul>
 * <p>
 * <b>NOTE</b> This method generates entropy fast but not necessarily
 * secure enough for seeding the random number generator.
 *
 * @return bytes of entropy
 */
private static byte[] getFastEntropy() {
    final MessageDigest md;

    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException nsae) {
        throw new InternalError("internal error: SHA-1 not available.");
    }

    // update with XorShifted time values
    update(md, System.currentTimeMillis());
    update(md, System.nanoTime());

    // scan the temp file system
    File file = new File(System.getProperty("java.io.tmpdir"));
    File[] entries = file.listFiles();
    if (entries != null) {
        for (File entry : entries) {
            md.update(entry.getName().getBytes());
            update(md, entry.lastModified());
            update(md, entry.length());
        }
    }

    return md.digest();
}

From source file:com.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from  w  w  w.  j a  v a  2 s  .  c o  m
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:ClassFinder.java

private static void findClassesInPathsDir(String strPathElement, File dir, Set<String> listClasses)
        throws IOException {
    String[] list = dir.list();//w  ww  . j av a2s  .com
    for (int i = 0; i < list.length; i++) {
        File file = new File(dir, list[i]);
        if (file.isDirectory()) {
            // Recursive call
            findClassesInPathsDir(strPathElement, file, listClasses);
        } else if (list[i].endsWith(DOT_CLASS) && file.exists() && (file.length() != 0)) {
            final String path = file.getPath();
            listClasses.add(path.substring(strPathElement.length() + 1, path.lastIndexOf(".")) // $NON-NLS-1$
                    .replace(File.separator.charAt(0), '.')); // $NON-NLS-1$
        } else if (list[i].endsWith(DOT_JAR) && file.exists() && (file.length() != 0)) {
            ZipFile zipFile = new ZipFile(file);
            Enumeration entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                String strEntry = entries.nextElement().toString();
                if (strEntry.endsWith(DOT_CLASS)) {
                    listClasses.add(fixClassName(strEntry));
                }
            }
            zipFile.close();
        }
    }
}