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 long getFileSize(File file) {
    long size = 0;
    for (File subFile : file.listFiles()) {
        if (subFile.isDirectory()) {
            size += getFileSize(subFile);
        } else {//from   w  w  w.j a v a 2  s  . c o m
            size += subFile.length();
        }
    }
    return size;
}

From source file:Main.java

public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException {
    Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm());

    String filename = url.getFile();
    int lastSlashPos = filename.lastIndexOf('/');
    String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1));

    File file = new File(cacheDir, fileNameNoPath);

    if (file.exists()) {
        if (file.length() > 0) {
            Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath());
            return file;
        } else {/*  w w  w  .  ja  v  a  2 s. c o  m*/
            Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath());
            file.delete();
        }
    }

    Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists.");

    URLConnection ucon = url.openConnection();
    ucon.setReadTimeout(5000);
    ucon.setConnectTimeout(30000);

    InputStream is = ucon.getInputStream();
    BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
    FileOutputStream outStream = new FileOutputStream(file);
    byte[] buff = new byte[5 * 1024];

    // Read bytes (and store them) until there is nothing more to read(-1)
    int len;
    while ((len = inStream.read(buff)) != -1) {
        outStream.write(buff, 0, len);
    }

    // Clean up
    outStream.flush();
    outStream.close();
    inStream.close();
    return file;
}

From source file:Main.java

public static String readFile(String filename) {
    String content = null;//from w  w w.  j  av  a  2  s  . c o m
    File file = new File(filename); //for ex foo.txt
    try {
        FileReader reader = new FileReader(file);
        char[] chars = new char[(int) file.length()];
        reader.read(chars);
        content = new String(chars);
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

public static long getFileSize(File file) throws Throwable {
    if (!file.exists()) {
        return 0L;
    } else if (!file.isDirectory()) {
        return file.length();
    } else {//from  w  w  w .j av  a 2 s.  co m
        String[] names = file.list();
        int size = 0;

        for (int i = 0; i < names.length; ++i) {
            File f = new File(file, names[i]);
            size = (int) ((long) size + getFileSize(f));
        }

        return (long) size;
    }
}

From source file:Main.java

/**
 * read one file and return its content as string
 * @param fileName/*from   w ww.  j a va  2s  . com*/
 * @return
 * @throws FileNotFoundException
 */
public static String readFileToString(String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    if (file.exists()) {
        StringBuilder stringBuilder = new StringBuilder((int) file.length());
        Scanner scanner = new Scanner(file, "UTF-8");
        String lineSeparator = System.getProperty("line.separator");

        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine() + lineSeparator);
        }

        scanner.close();
        return stringBuilder.toString();
    }

    Log.e(TAG_CLASS, "no file called: " + fileName);
    return null;
}

From source file:com.frostwire.search.tests.KATSearchTest.java

private static boolean testOnSearchResults(List<KATSearchResult> results) throws IOException {
    final boolean failed = true;
    for (KATSearchResult result : results) {
        System.out.println(result.getDetailsUrl());
        System.out.println(result.getTorrentUrl());
        System.out.println(result.getDisplayName());
        System.out.println(result.getFilename());
        System.out.println(result.getSource());
        System.out.println();/* w w w .j av a2s  .c  o  m*/
        String torrentFileName = result.getFilename().replace("\\s", "_") + ".txt";
        //torcache.net's landing page can be removed by passing an Http Referrer equal to "https://torcache.net/"
        System.out.println("Connecting to " + result.getTorrentUrl());
        getHttpClient().save(result.getTorrentUrl(), new File(torrentFileName), false, 10000, "",
                "https://torcache.net/");
        File savedTorrent = new File(torrentFileName);
        if (savedTorrent.exists() && savedTorrent.length() > 0
                && FileUtils.readFileToString(savedTorrent).contains("<html")) {
            return failed;
        }

        System.out.println("Saved .torrent -> " + savedTorrent.getAbsolutePath());
        System.out.println("============================\n");
    }

    return !failed;
}

From source file:Main.java

public static int getFileSize(File file) {
    int result = 0;
    if (file == null) {
        return result;
    }/*from   w  ww. jav  a  2  s  .  com*/
    if (!file.isDirectory()) {
        return (int) file.length();
    }
    if (file.listFiles() == null) {
        return result;
    }
    for (File item : file.listFiles()) {
        result += getFileSize(item);
    }
    return result;
}

From source file:Main.java

private static long calculateDirectorySize(File directory) {
    long result = 0;
    for (File file : directory.listFiles()) {
        if (file.isDirectory()) {
            result += calculateDirectorySize(file);
        } else {//ww w.j  a  v a2  s.  c o m
            result += file.length();
        }
    }
    return result;
}

From source file:Main.java

/**
 * (java) read from file/*  w w  w  .  ja  v  a  2  s  . c o m*/
 * 
 * @param path
 * @return
 */
public static String readFile(File file) {
    String str = "";
    FileInputStream in;
    try {
        in = new FileInputStream(file);
        int length = (int) file.length();
        byte[] temp = new byte[length];
        in.read(temp, 0, length);
        str = EncodingUtils.getString(temp, FILE_ENCODING);
        in.close();
    } catch (IOException e) {
        Log.e("", "read error!");

    }
    return str;
}

From source file:Main.java

private static void copyAsset(AssetManager am, File rep, boolean force) {
    try {/*  w w  w .  ja  v 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]);
                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();
    }
}