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:cn.edu.xmu.tidems.control.support.TideMSUtil.java

public static boolean isHDF5File(final File file) {

    if ((!file.exists()) || (file.isDirectory()) || (file.length() < 1024)) {
        return false;
    }/*from w w w.  j av a  2 s.com*/
    try {
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        final long sig = raf.readLong(); // The first 8 byte are format signatures of hdf5 file
        raf.close();
        return sig == -8554512424533091830L; // 0x89 48 44 46 0d 0a 1a 0a
    } catch (final Exception e) {
        return false;
    }
}

From source file:com.izforge.izpack.test.util.TestHelper.java

/**
 * Verifies that two files have the same content.
 * <p/>//  w ww .java2 s  . c  o m
 * The files must have different paths.
 *
 * @param expected the expected file
 * @param actual   the actual file
 */
public static void assertFileEquals(File expected, File actual) {
    assertTrue(actual.exists());
    assertFalse(actual.getAbsolutePath().equals(expected.getAbsolutePath()));
    assertEquals(expected.length(), actual.length());
    try {
        assertEquals(FileUtils.checksumCRC32(expected), FileUtils.checksumCRC32(actual));
    } catch (IOException exception) {
        fail(exception.getMessage());
    }
}

From source file:com.o2d.pkayjava.editor.utils.ImportUtils.java

public static int checkFileTypeByContent(String path) {
    File file = new File(path);
    long fileSizeInBytes = file.length();
    // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
    long fileSizeInKB = fileSizeInBytes / 1024;

    if (fileSizeInKB > 200) {
        return TYPE_UNCKNOWN;
    }/*from  w  w  w .j a  va2s  .c om*/

    int type = TYPE_UNCKNOWN;

    try {
        String contents = FileUtils.readFileToString(file);

        // checking for atlas file
        if (contents.contains("format: ") && contents.contains("filter: ") && contents.contains("xy: ")) {
            type = TYPE_TEXTURE_ATLAS;
            // need to figure out if atlas is animation or just files.
            TextureAtlas atlas = new TextureAtlas(new FileHandle(file));
            String[] regionNames = new String[atlas.getRegions().size];
            for (int i = 0; i < atlas.getRegions().size; i++) {
                regionNames[i] = atlas.getRegions().get(i).name;
            }
            boolean isSequence = isAnimationSequence(regionNames);

            if (isSequence) {
                type = TYPE_SPRITE_ANIMATION_ATLAS;
            }

            return type;
        }

        // checking for spine animation
        if (contents.contains("\"skeleton\":{\"")) {
            type = TYPE_SPINE_ANIMATION;
            return type;
        }

        // checking for particle effect
        if (contents.contains("- Options - ") && contents.contains("- Image Path -")
                && contents.contains("- Duration -")) {
            type = TYPE_PARTICLE_EFFECT;
            return type;
        }

    } catch (IOException e) {
    }

    return type;
}

From source file:com.uwsoft.editor.utils.ImportUtils.java

public static int checkFileTypeByContent(String path) {
    File file = new File(path);
    long fileSizeInBytes = file.length();
    // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
    long fileSizeInKB = fileSizeInBytes / 1024;

    if (fileSizeInKB > 1000) {
        return TYPE_UNCKNOWN;
    }//  w w w.  ja va 2 s . c o m

    int type = TYPE_UNCKNOWN;

    try {
        String contents = FileUtils.readFileToString(file);

        // checking for atlas file
        if (contents.contains("format: ") && contents.contains("filter: ") && contents.contains("xy: ")) {
            type = TYPE_TEXTURE_ATLAS;
            // need to figure out if atlas is animation or just files.
            TextureAtlas atlas = new TextureAtlas(new FileHandle(file));
            String[] regionNames = new String[atlas.getRegions().size];
            for (int i = 0; i < atlas.getRegions().size; i++) {
                regionNames[i] = atlas.getRegions().get(i).name;
            }
            boolean isSequence = isAnimationSequence(regionNames);

            if (isSequence) {
                type = TYPE_SPRITE_ANIMATION_ATLAS;
            }

            return type;
        }

        // checking for spine animation
        if (contents.contains("\"skeleton\":{\"") || contents.contains("\"skeleton\": { \"")
                || contents.contains("{\"bones\":[")) {
            type = TYPE_SPINE_ANIMATION;
            return type;
        }

        // checking for particle effect
        if (contents.contains("- Options - ") && contents.contains("- Image Path -")
                && contents.contains("- Duration -")) {
            type = TYPE_PARTICLE_EFFECT;
            return type;
        }

    } catch (IOException e) {
    }

    return type;
}

From source file:com.plotsquared.iserver.util.FileUtils.java

/**
 * Get the size of a file or directory//from  w  ww. j  a  v a 2s  . c  o  m
 *
 * @param file File
 * @return Size of file
 */
public static long getSize(final File file) {
    long size = 0;
    if (file.isDirectory()) {
        final File[] files = file.listFiles();
        if (files == null)
            return size;
        for (final File f : files)
            if (f.isFile())
                size += f.length();
            else
                size += getSize(file);
    } else if (file.isFile())
        size += file.length();
    return size;
}

From source file:com.doplgangr.secrecy.utils.Util.java

public static Map<String, File> getAllStorageLocations() {
    Map<String, File> map = new TreeMap<String, File>();

    List<String> mMounts = new ArrayList<String>(99);
    //List<String> mVold = new ArrayList<String>(99);
    mMounts.add(Environment.getExternalStorageDirectory().getAbsolutePath());
    try {//from   w  w  w .jav  a 2 s .  c o m
        File mountFile = new File("/proc/mounts");
        if (mountFile.exists()) {
            Scanner scanner = new Scanner(mountFile);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                //if (line.startsWith("/dev/block/vold/")) {
                String[] lineElements = line.split(" ");
                String element = lineElements[1];
                mMounts.add(element);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    List<String> mountHash = new ArrayList<String>(99);

    for (String mount : mMounts) {
        File root = new File(mount);
        Util.log(mount, "is checked");
        Util.log(mount, root.exists(), root.isDirectory(), canWrite(root));
        if (canWrite(root)) {
            Util.log(mount, "is writable");
            File[] list = root.listFiles();
            String hash = "[";
            if (list != null)
                for (File f : list)
                    hash += f.getName().hashCode() + ":" + f.length() + ", ";
            hash += "]";
            if (!mountHash.contains(hash)) {
                String key = root.getAbsolutePath() + " ("
                        + org.apache.commons.io.FileUtils.byteCountToDisplaySize(root.getUsableSpace())
                        + " free space)";
                mountHash.add(hash);
                map.put(key, root);
            }
        }
    }

    mMounts.clear();
    return map;
}

From source file:Main.java

public static byte[] readByteArray(File file) {
    if (!file.exists() || !file.isFile()) {
        return null;
    }/*w  ww.j  a  va 2  s . c om*/
    byte[] data = null;
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(file);
        data = new byte[(int) file.length()];
        stream.read(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}

From source file:de.uzk.hki.da.sb.Utilities.java

/**
 * Checks if zero byte files exist in the given folder 
 * //  ww w  . j av  a  2  s .  c o m
 * @param folder The folder to check
 * @param sipName The SIP name
 * @param messageWriter The message writer
 * @return true if zero byte files exist inside the folder, false otherwise
 */
public static boolean checkForZeroByteFiles(File folder, String sipName, MessageWriter messageWriter) {

    Collection<File> files = FileUtils.listFiles(folder, null, true);

    for (File file : files) {
        if (file.length() == 0) {
            String zeroByteFileEntry = file.getName() + " (" + sipName + ")";
            messageWriter.addZeroByteFile(zeroByteFileEntry);
        }
    }

    if (messageWriter.getZeroByteFiles().size() > 0)
        return true;
    else
        return false;
}

From source file:com.technofovea.packbsp.crawling.AssetAlternative.java

/**
 * Taking an array of alternative items, tests if there is a
 * @param items/*w w  w  .j  ava2  s  . c o m*/
 * @return
 * @throws IOException
 */
public static CollisionType hasCustomCollision(List<AssetAlternative> items) throws IOException {
    //TODO same-hash in BSP+Archive is a bad idea since it means the map is not forward-compatible. Fix in future
    if (items.size() < 2) {
        // Nothing to even check against
        return CollisionType.NONE;
    }

    // Test if our first-level item collides with the first ARCHIVE-type things below it

    AssetAlternative topLevelItem = items.get(0);
    AssetAlternative archivedItem = null;
    for (int i = 1; i < items.size(); i++) {
        AssetAlternative next = items.get(i);
        if (next.getType().equals(Type.ARCHIVE)) {
            archivedItem = next;
            break;
        }
    }
    if (archivedItem == null) {
        // No secondary item was found that was an archive-type
        // Nothing relevant to collide with
        return CollisionType.NONE;
    }

    // Basic size check
    File diskFile = topLevelItem.getFile();
    File archiveFile = archivedItem.getFile();

    boolean sameSize = (diskFile.length() == archiveFile.length());
    boolean sameData = false;
    if (sameSize) {
        // Tougher check involving actual contents
        FileInputStream diskStream = new FileInputStream(diskFile);
        FileInputStream archiveStream = new FileInputStream(archiveFile);
        sameData = IOUtils.contentEquals(diskStream, archiveStream);
        diskStream.close();
        archiveStream.close();
    }

    if (sameData) {
        return CollisionType.DUPLICATE;
    } else {
        return CollisionType.CONFLICTING;
    }

}

From source file:eu.planets_project.pp.plato.util.FileUtils.java

public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);
    byte[] bytes;
    try {/*  w w w  .  j  a  v  a 2 s. c  o  m*/

        // Get the size of the file
        long length = file.length();

        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        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) {
            throw new IOException("Could not completely read file " + file.getName());
        }

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