Java File Size Get getFileSize(String path)

Here you can find the source of getFileSize(String path)

Description

Returns the size of the specified file in bytes.

License

Open Source License

Parameter

Parameter Description
path the path to the file.

Return

the size of the file in bytes.

Declaration

public static long getFileSize(String path) 

Method Source Code

//package com.java2s;
/**/*from w  w w.j a va2s.co m*/
 *
 * This file is part of Stuffed.
 *
 * Stuffed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Stuffed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stuffed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * File name:    Common.java
 * Package: cs.man.ac.uk.common
 * Created:   May 1, 2013
 * Author:   Rob Lyon
 * 
 * Contact:   rob@scienceguyrob.com or robert.lyon@postgrad.manchester.ac.uk
 * Web:      <http://www.scienceguyrob.com> or <http://www.cs.manchester.ac.uk> 
 *          or <http://www.jb.man.ac.uk>
 */

import java.io.File;

public class Main {
    /**
     * Returns the size of the specified file in bytes.
     * @param path the path to the file.
     * @return the size of the file in bytes.
     */
    public static long getFileSize(String path) {
        try {
            File file = new File(path);
            long fileSize = file.length();
            return fileSize;
        } catch (Exception e) {
            System.out.println("Could not obtain file size:\n" + e.toString());
            return -1;
        }
    }
}

Related

  1. getFileSize(String filePath)
  2. getFileSize(String fileSize)
  3. getFileSize(String fn)
  4. getFileSize(String loc)
  5. getFileSize(String path)
  6. getFilesize(String path)
  7. getFileSize2(String filename)
  8. getFileSizeAggregate(String[] files)
  9. getFileSizeFormat(double inputvalue)