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:com.jkoolcloud.tnt4j.streams.inputs.FileLineStream.java

private static int[] getFilesTotals(File[] activityFiles) {
    int tbc = 0;/*from   ww  w .ja  va  2s.c  o m*/
    int tlc = 0;
    if (ArrayUtils.isNotEmpty(activityFiles)) {
        for (File f : activityFiles) {
            tbc += f.length();
            try {
                tlc += Utils.countLines(new FileInputStream(f));
            } catch (IOException exc) {
            }
        }
    }

    return new int[] { tbc, tlc };
}

From source file:importer.handler.post.stages.Splitter.java

private static String readConfig(String fName) throws IOException {
    File f = new File(fName);
    FileReader fr = new FileReader(f);
    char[] data = new char[(int) f.length()];
    fr.read(data);//from  www.jav a2s  .  com
    // use platform encoding - pretty simple
    return new String(data);
}

From source file:Main.java

public static Bitmap getBitmapFromFile(File file, int w) {
    FileInputStream fileInputStream = null;
    try {/*from   ww  w  . j  a  va 2s. co m*/
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        if (file == null || !file.exists()) {
            return null;
        } else if (file.length() == 0) {
            file.delete();
            return null;
        }
        fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
        int be = getSampleSize(opts.outWidth, w);
        opts.inSampleSize = be;
        opts.inJustDecodeBounds = false;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeStream(fileInputStream, null, opts);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:com.jimplush.goose.images.ImageSaver.java

/**
 * stores an image to disk and returns the path where the file was written
 *
 * @param imageSrc//  w ww .j  a  v a2  s .  c o m
 * @return
 */
public static String storeTempImage(HttpClient httpClient, String linkhash, String imageSrc,
        Configuration config) throws SecretGifException {

    String localSrcPath = null;
    HttpGet httpget = null;
    HttpResponse response = null;

    try {

        imageSrc = imageSrc.replace(" ", "%20");
        if (logger.isDebugEnabled()) {
            logger.debug("Starting to download image: " + imageSrc);
        }

        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, HtmlFetcher.emptyCookieStore);

        httpget = new HttpGet(imageSrc);

        response = httpClient.execute(httpget, localContext);

        String respStatus = response.getStatusLine().toString();
        if (!respStatus.contains("200")) {
            return null;
        }

        HttpEntity entity = response.getEntity();

        String fileExtension = "";
        try {
            Header contentType = entity.getContentType();
        } catch (Exception e) {
            logger.error(e.getMessage());

        }

        // generate random token
        Random generator = new Random();
        int randInt = generator.nextInt();

        localSrcPath = config.getLocalStoragePath() + "/" + linkhash + "_" + randInt;

        if (logger.isDebugEnabled()) {
            logger.debug("Storing image locally: " + localSrcPath);
        }
        if (entity != null) {
            InputStream instream = entity.getContent();
            OutputStream outstream = new FileOutputStream(localSrcPath);
            try {
                try {
                    IOUtils.copy(instream, outstream);
                } catch (Exception e) {
                    throw e;
                } finally {
                    entity.consumeContent();
                    instream.close();
                    outstream.close();
                }

                // get mime type and store the image extension based on that shiz
                fileExtension = ImageSaver.getFileExtension(config, localSrcPath);
                if (fileExtension == "" || fileExtension == null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("EMPTY FILE EXTENSION: " + localSrcPath);
                    }
                    return null;
                }
                File f = new File(localSrcPath);
                if (f.length() < config.getMinBytesForImages()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("TOO SMALL AN IMAGE: " + localSrcPath + " bytes: " + f.length());
                    }
                    return null;
                }

                File newFile = new File(localSrcPath + fileExtension);
                f.renameTo(newFile);
                localSrcPath = localSrcPath + fileExtension;

                if (logger.isDebugEnabled()) {
                    logger.debug("Image successfully Written to Disk");
                }

            } catch (IOException e) {
                logger.error(e.toString(), e);
            } catch (SecretGifException e) {
                throw e;
            } catch (Exception e) {
                logger.error(e.getMessage());
            }

        }

    } catch (IllegalArgumentException e) {
        logger.warn(e.getMessage());
    } catch (SecretGifException e) {
        raise(e);
    } catch (ClientProtocolException e) {

        logger.error(e.toString());
    } catch (IOException e) {
        logger.error(e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.toString());
        e.printStackTrace();
    } finally {

        httpget.abort();

    }

    return localSrcPath;
}

From source file:Main.java

/**
 * Returns the size of the specified file or directory. If the provided 
 * {@link File} is a regular file, then the file's length is returned.
 * If the argument is a directory, then the size of the directory is
 * calculated recursively. If a directory or subdirectory is security 
 * restricted, its size will not be included.
 * /*from ww  w . j  ava 2s. co m*/
 * @param file the regular file or directory to return the size 
 *        of (must not be <code>null</code>).
 * 
 * @return the length of the file, or recursive size of the directory, 
 *         provided (in bytes).
 * 
 * @throws NullPointerException if the file is <code>null</code>
 * @throws IllegalArgumentException if the file does not exist.
 *         
 * @since Commons IO 2.0
 */
public static long sizeOf(File file) {

    if (!file.exists()) {
        String message = file + " does not exist";
        throw new IllegalArgumentException(message);
    }

    if (file.isDirectory()) {
        return sizeOfDirectory(file);
    } else {
        return file.length();
    }

}

From source file:com.bc.util.io.FileUtils.java

public static void move(File source, File target) throws IOException {
    copy(source, target);/*from w  w w  .  j  a va2s.c om*/

    if (source.length() == target.length()) {
        source.delete();
    }
}

From source file:com.mbeddr.pluginmanager.com.intellij.ide.plugins.RepositoryHelper.java

/**
 * Reads cached plugin descriptors from a file. Returns null if cache file does not exist.
 *//*from  w  ww. j ava 2  s  .c o  m*/
@Nullable
public static List<IdeaPluginDescriptor> loadCachedPlugins() throws IOException {
    File file = new File(PathManager.getPluginsPath(), PLUGIN_LIST_FILE);
    return file.length() == 0 ? null : loadPluginList(file);
}

From source file:ImageEncode.java

public static byte[] getBytesFromFile(File file) throws IOException {
    final InputStream is = new FileInputStream(file);
    // Get the size of the file
    final long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
        is.close();//  w  ww.ja  v  a 2s .c o  m
        throw new IOException("File exceeds max value: " + Integer.MAX_VALUE);
    }

    // Create the byte array to hold the data
    final byte[] bytes = new byte[(int) length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        is.close();
        throw new IOException("Could not completely read file" + file.getName());

    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}

From source file:com.flexive.shared.FxFileUtils.java

/**
 * Compare if two files match. Currently this method loads both files into memory and compares them,
 * so don't use this on arbitrarily large files.
 *
 * @param file1 first file to compare/*from  ww w.ja v a2s .  com*/
 * @param file2 second file to compare
 * @return match
 */
public static boolean fileCompare(File file1, File file2) {
    try {
        return file2.length() == file1.length() && Arrays.equals(getBytes(file2), getBytes(file1));
    } catch (IOException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Failed to compare files: " + e.getMessage(), e);
        }
        return false;
    }
}

From source file:imageencode.ImageEncode.java

public static byte[] getBytesFromFile(final File file) throws IOException {
    final InputStream is = new FileInputStream(file);
    // Get the size of the file
    final long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
        is.close();//from ww w  .java2s  . c  om
        throw new IOException("File exceeds max value: " + Integer.MAX_VALUE);
    }

    // Create the byte array to hold the data
    final byte[] bytes = new byte[(int) length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        is.close();
        throw new IOException("Could not completely read file" + file.getName());

    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}