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

private static long getFolderSize(File file) {
    long size = 0;
    File[] fileList = file.listFiles();
    for (File aFileList : fileList) {
        if (aFileList.isDirectory()) {
            size = size + getFolderSize(aFileList);
        } else {/*from  w ww .  j  a  va 2s.  c  om*/
            size = size + aFileList.length();
        }
    }
    return size;
}

From source file:Main.java

public static boolean isFileBiggerThan1MB(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;/*from   w w w.java2  s  .  c o  m*/
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    return streamLength >= MAX_IMAGE_SIZE;
}

From source file:TestMapObjInterface.java

public static boolean runTest(String file, String fsName, String cnaddress) {
    try {//from w  w  w .j  a  va 2 s  .  co  m
        long targetNumChunks = 2;
        FileWriter godot = new FileWriter(file);
        File monet = new File(file);
        godot.write("I'm waiting.");
        godot.flush();
        while ((monet.length() / CHUNK_SIZE + 1) < targetNumChunks) {
            godot.write("I'm waiting.");
        }
        godot.flush();
        godot.close();
        ArrayList<String> chunks = getChunks(file, fsName, cnaddress);
        long fileChunks = (monet.length() / CHUNK_SIZE) + 1;
        if (fileChunks == chunks.size()) {
            return true;
        } else {
            System.out.print("expected " + fileChunks + " chunks, got " + chunks.size() + ": ");
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:mobile.controllers.MobileApp.java

@BodyParser.Of(value = BodyParser.Raw.class, maxLength = 5 * 1024 * 1024)
public static Result uploadLog(String from, String comment) {
    if (request().body().isMaxSizeExceeded()) {
        return MobileResult.error("290001", "?5M").getResult();
    }/*  w ww. ja va2 s .  co m*/

    if (StringUtils.isNotBlank(comment)) {
        try {
            comment = URLDecoder.decode(comment, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("url?", e);
        }
    }

    File file = request().body().asRaw().asFile();
    if (null == file || file.length() <= 0) {
        return MobileResult.error("100005", "?0").getResult();
    }

    ClientLogService.uploadLog(from, file, comment);

    return MobileResult.success().getResult();
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();/* w  w w. java 2 s .  co m*/
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:Main.java

private static long getSizeOfFileOrDirectoryInByte(File fileOrDirectory) {
    if (!fileOrDirectory.exists()) {
        return 0;
    }//from ww  w  .jav  a2s.co m
    if (fileOrDirectory.isFile()) {
        return fileOrDirectory.length();
    }

    File[] contents = fileOrDirectory.listFiles();
    long size = 0;
    for (File file : contents) {
        size += file.isDirectory() ? getSizeOfFileOrDirectoryInByte(file) : file.length();
    }
    return size;
}

From source file:Main.java

public static long getFileSize(File dirOrFile) {

    if (dirOrFile == null || !dirOrFile.exists()) {
        return 0;
    }/*from w ww . j  a va2  s.  c om*/

    if (dirOrFile.isFile()) {
        return dirOrFile.length();
    }

    if (dirOrFile.isDirectory()) {
        File[] subDirOrFiles = dirOrFile.listFiles();
        if (subDirOrFiles == null || subDirOrFiles.length <= 0) {
            return 0;
        }
        long size = 0;
        for (File subDirOrFile : subDirOrFiles) {
            size += getFileSize(subDirOrFile);
        }
        return size;
    }

    return 0;
}

From source file:DuplicatedMusicCleaner.java

public static void cleanupFolder(File folder) throws IOException {
    // System.out.println("Evaluate " + folder.getAbsolutePath());
    String[] fileNames = folder.list();
    Map<String, String> fileNamesWithoutExtension = new HashMap<String, String>();
    for (String fileName : fileNames) {
        File file = new File(folder, fileName);
        String nameWithoutExtension = StringUtils.substringBeforeLast(file.getName(), ".");
        fileNamesWithoutExtension.put(nameWithoutExtension, file.getAbsolutePath());
    }/*from  w w w . j a  va2 s. co m*/

    for (String fileName : fileNames) {
        // System.out.println("Evaluate " + fileName);
        File file = new File(folder, fileName);
        if (file.isDirectory()) {
            cleanupFolder(file);
        } else {

            String nameWithoutExtension = StringUtils.substringBeforeLast(file.getName(), ".");
            String suffix = " 1";
            if (nameWithoutExtension.endsWith(suffix)) {
                String nameWithoutSuffix = StringUtils.substringBeforeLast(nameWithoutExtension, suffix);
                if (fileNamesWithoutExtension.keySet().contains(nameWithoutSuffix)) {
                    File originalFile = new File(fileNamesWithoutExtension.get(nameWithoutSuffix));
                    long originalFileLength = originalFile.length();
                    long fileLength = file.length();
                    if (originalFileLength == fileLength) {
                        System.err.println("Delete " + file.getName());
                        FileUtils.deleteQuietly(file);
                        // FileUtils.moveFile(file, new
                        // File(file.getAbsoluteFile() + ".todelete"));
                    }
                }
            }
            /*
             * if(file.getName().endsWith(".todelete")) { String newFileName
             * = StringUtils.substringBeforeLast(file.getName(),
             * ".todelete"); File parent = file.getParentFile(); File
             * parentParent = parent.getParentFile(); newFileName =
             * parentParent.getName() + "_" + parent.getName() + "_" +
             * newFileName;
             * 
             * File target = new File("/Users/cyrilleleclerc/todelete",
             * newFileName); System.out.println(target);
             * FileUtils.moveFile(file, target);
             * 
             * }
             */
        }
    }

}

From source file:melnorme.utilbox.misc.FileUtil.java

/** Read all bytes of the given file. 
 * @return the bytes that where read in a {@link java.io.ByteArrayOutputStream}. */
public static IByteSequence readBytesFromFile(File file) throws IOException, FileNotFoundException {
    long fileLength = file.length();
    /*/*www .ja  v a2 s  .c o  m*/
     * 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 (fileLength > Integer.MAX_VALUE)
        throw new IOException("File is too large, size is bigger than " + Integer.MAX_VALUE);

    return StreamUtil.readAllBytesFromStream(new FileInputStream(file), (int) fileLength);
}

From source file:ispyb.common.util.IspybFileUtils.java

public static byte[] getFile(String filePath) throws IOException {
    File file = new File(filePath);
    if (file.exists()) {
        byte[] fileInBytes = new byte[(int) file.length()];
        InputStream inputStream = null;
        try {//www . ja  v a  2s  .c  o  m
            inputStream = new FileInputStream(file);
            inputStream.read(fileInBytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw e;
        } finally {
            inputStream.close();
        }
        return fileInBytes;
    }
    return null;
}