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:Main.java

public static String loadFile(File f) throws IOException {
    FileInputStream stream = new FileInputStream(f);
    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
    char[] cData = new char[(int) f.length()];
    reader.read(cData);//  w w w  .  j a v a2s  . c om
    return new String(cData).trim();
}

From source file:br.com.edu.dbpediaspotlight.AnnotationClient.java

protected static String readFileAsString(File file) throws IOException {
    byte[] buffer = new byte[(int) file.length()];
    @SuppressWarnings("resource")
    BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
    f.read(buffer);//from   ww  w  .  ja v a2  s . c  om
    return new String(buffer);
}

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static String readFileContent(URI uri) throws IOException {
    File file = new File(uri);
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);/*w w w  . j a  v a 2s  .c  o  m*/
    is.close();
    return new String(b);
}

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static byte[] readFileContent(File file) throws IOException {
    byte[] b = new byte[(int) file.length()];
    new FileInputStream(file).read(b);
    return b;//from w ww .j a v a 2s.c  o  m
}

From source file:Main.java

public static byte[] getFileBytes(File file) throws IOException {
    BufferedInputStream bis = null;
    try {//from   ww w  .  ja v  a 2s  .  co m
        bis = new BufferedInputStream(new FileInputStream(file));
        int bytes = (int) file.length();
        byte[] buffer = new byte[bytes];
        int readBytes = bis.read(buffer);
        if (readBytes != buffer.length) {
            throw new IOException("Entire file not read");
        }
        return buffer;
    } finally {
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

/**
 * in KB/* w w w .  j  a va 2  s  . c om*/
 *
 * @return KB
 */
public static long getCacheSize() {
    long sum = 0;
    if (filesInCache == null) {
        return 0;
    } else {
        for (File file : filesInCache) {

            if (!file.exists())
                continue;
            if (file.isFile()) {
                sum += file.length();
                //LogUtils.i(TAG, file.getAbsolutePath() + "isFile");
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (!files[i].exists())
                        continue;
                    if (files[i].isFile()) {
                        //LogUtils.i(TAG, file.getAbsolutePath() + "isDir");
                        sum += file.length();
                    }
                }
            }
        }
    }
    return sum / 1024;
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {
    File file = new File(path);
    response.setBufferSize((int) file.length());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    response.setContentType(contentType);
    response.setContentLength((int) file.length());

    byte[] bytes = new byte[(int) file.length()];
    FileInputStream fis = new FileInputStream(file);
    fis.read(bytes);//from   www  .jav a2s  .c om
    fis.close();

    FileCopyUtils.copy(bytes, response.getOutputStream());
}

From source file:Main.java

public static long calcFileSpace(final File dir) {
    if (!dir.exists()) {
        return 0;
    }//  w w w  . j  a  v  a2  s  . c o m
    long ret = 0;
    if (dir.isDirectory()) {
        final File[] filies = dir.listFiles();
        for (int i = 0; i < filies.length; i++) {
            final File curFile = filies[i];
            if (curFile.isDirectory()) {
                ret += calcFileSpace(curFile);
            } else {
                ret += curFile.length();
            }
        }
    }
    return ret;
}

From source file:com.grapeshot.halfnes.FileUtils.java

public static int[] readfromfile(final String path) {
    File f = new File(path);
    byte[] bytes = new byte[(int) f.length()];
    FileInputStream fis;// w ww  . ja va 2 s . c  om
    try {
        fis = new FileInputStream(f);
        fis.read(bytes);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.err.println("Failed to load file");
        e.printStackTrace();
    }
    int[] ints = new int[bytes.length];

    for (int i = 0; i < bytes.length; i++) {
        ints[i] = (short) (bytes[i] & 0xFF);
    }

    return ints;
}

From source file:com.mentor.questa.jenkins.Util.java

/**
 * The method returns the possibly trimmed file contents.
 *
 * @param keepLongStdio if true, the file is eligible for trimming
 * @param stdio/*from w  ww  .ja  va  2 s .  c  om*/
 * @return
 * @throws IOException
 */
private static String possiblyTrimStdio(boolean keepLongStdio, File stdio) throws IOException {
    final long len = stdio.length();
    if (keepLongStdio && len < 1024 * 1024) {
        return FileUtils.readFileToString(stdio);
    }

    final int halfMaxSize = 5000;

    long middle = len - halfMaxSize * 2;
    String head = "", tail = "";
    if (middle > 0) {
        TextFile tx = new TextFile(stdio);
        head = tx.head(halfMaxSize);
        tail = tx.fastTail(halfMaxSize);

        int headBytes = head.getBytes().length;
        int tailBytes = tail.getBytes().length;

        middle = len - (headBytes + tailBytes);
    }

    if (middle <= 0) {
        return FileUtils.readFileToString(stdio);
    }

    return head + "\n...[truncated " + middle + " bytes]...\n" + tail;
}