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.haulmont.cuba.core.sys.logging.LogArchiver.java

private static ArchiveEntry newArchive(File file) throws IOException {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(file.getName());
    zipEntry.setSize(file.length());
    zipEntry.setCompressedSize(zipEntry.getSize());
    zipEntry.setCrc(FileUtils.checksumCRC32(file));
    return zipEntry;
}

From source file:avantssar.aslanpp.testing.Tester.java

public static File deleteIfZero(File f) {
    if (f != null && f.length() == 0) {
        if (f.delete()) {
            return null;
        }/*from   w w w. j a v  a 2s. co  m*/
    }
    return f;
}

From source file:org.phpmaven.pear.library.impl.Helper.java

/**
 * Returns the binary file contents./*  ww  w .ja  v a  2 s.c o  m*/
 * @param uri URI of the resource.
 * @return the files content.
 * @throws IOException thrown on errors.
 */
public static byte[] getBinaryFileContents(String uri) throws IOException {
    // is it inside the local filesystem?
    if (uri.startsWith("file://")) {
        final File channelFile = new File(uri.substring(7));

        final byte[] result = new byte[(int) channelFile.length()];
        final FileInputStream fis = new FileInputStream(channelFile);
        fis.read(result);
        fis.close();
        return result;
    }

    // try http connection
    final HttpClient client = new DefaultHttpClient();
    final HttpGet httpget = new HttpGet(uri);
    final HttpResponse response = client.execute(httpget);
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / "
                + response.getStatusLine().getReasonPhrase());
    }
    final HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new IOException("Empty response.");
    }
    return EntityUtils.toByteArray(entity);
}

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

public static void replaceAllInFile(File file, String oldValue, String newValue) throws IOException {
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);/*from   ww w  . j  a v a 2s.  c o  m*/
    is.close();
    String s = new String(b);
    s = s.replaceAll(oldValue, newValue);
    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();
}

From source file:hoot.services.controllers.info.ErrorLog.java

static String getErrorlog(int maxLength) throws IOException {
    File file = new File(ERROR_LOG_PATH);

    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
        long fileLength = file.length();

        long startOffset = 0;
        if (fileLength > maxLength) {
            startOffset = fileLength - maxLength;
        }/*w  w  w.  j ava  2 s . com*/

        randomAccessFile.seek(startOffset);

        byte[] buffer = new byte[maxLength];
        randomAccessFile.read(buffer, 0, maxLength);

        return new String(buffer);
    }
}

From source file:Main.java

private static boolean copyAsset(AssetManager am, File rep, boolean force) {
    boolean existingInstall = false;
    try {/*from w  ww. j  av a 2 s. com*/
        String assets[] = am.list("");
        for (int i = 0; i < assets.length; i++) {
            boolean hp48 = assets[i].equals("hp48");
            boolean hp48s = assets[i].equals("hp48s");
            boolean ram = assets[i].equals("ram");
            boolean rom = assets[i].equals("rom");
            boolean rams = assets[i].equals("rams");
            boolean roms = assets[i].equals("roms");
            int required = 0;
            if (ram)
                required = 131072;
            else if (rams)
                required = 32768;
            else if (rom)
                required = 524288;
            else if (roms)
                required = 262144;
            //boolean SKUNK = assets[i].equals("SKUNK");
            if (hp48 || rom || ram || hp48s || roms || rams) {
                File fout = new File(rep, assets[i]);
                existingInstall |= fout.exists();
                if (!fout.exists() || fout.length() == 0 || (required > 0 && fout.length() != required)
                        || force) {
                    Log.i("x48", "Overwriting " + assets[i]);
                    FileOutputStream out = new FileOutputStream(fout);
                    InputStream in = am.open(assets[i]);
                    byte buffer[] = new byte[8192];
                    int n = -1;
                    while ((n = in.read(buffer)) > -1) {
                        out.write(buffer, 0, n);
                    }
                    out.close();
                    in.close();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return existingInstall;
}

From source file:IORoutines.java

public static byte[] load(File file) throws IOException {
    long fileLength = file.length();
    if (fileLength > Integer.MAX_VALUE) {
        throw new IOException("File '" + file.getName() + "' too big");
    }//from ww w .  j  ava 2 s  .  c o m
    InputStream in = new FileInputStream(file);
    try {
        return loadExact(in, (int) fileLength);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String getFileMD5String(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    FileChannel ch = inputStream.getChannel();
    MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    messagedigest.update(byteBuffer);//  w w w  .  java  2s  .  c  om
    return bufferToHex(messagedigest.digest());
}

From source file:com.destroystokyo.paperclip.Paperclip.java

private static byte[] getBytes(final File file) throws IOException {
    final byte[] b = new byte[(int) file.length()];

    try (final FileInputStream fis = new FileInputStream(file)) {
        if (fis.read(b) != b.length) {
            System.err.println("Error reading all the data from " + file.getAbsolutePath());
            System.exit(1);/*from   w w w  .j  av  a  2s. co m*/
        }
    }

    return b;
}

From source file:fm.last.commons.test.TestAssert.java

/**
 * Asserts that the *contents* of the passed files are equal.
 * /*  w  w w. j a  va2 s  .co m*/
 * @param message Failure message.
 * @param expectedFile File containing expected data.
 * @param actualFile File to compare.
 * @throws IOException If an error occurs comparing the file contents.
 */
public static void assertFileEquals(String message, File expectedFile, File actualFile) throws IOException {
    assertEquals(message, expectedFile.length(), actualFile.length());

    InputStream expected = null;
    InputStream actual = null;
    boolean equal = true;
    try {
        expected = new BufferedInputStream(new FileInputStream(expectedFile));
        actual = new BufferedInputStream(new FileInputStream(actualFile));
        int readExpected;
        int readActual;
        do {
            readActual = actual.read();
            readExpected = expected.read();
            if (readActual != readExpected) {
                equal = false;
                break;
            }
        } while (readExpected != -1 && readActual != -1);
    } finally {
        IOUtils.closeQuietly(expected);
        IOUtils.closeQuietly(actual);
    }
    if (!equal) {
        fail(message);
    }
}