Get file size : Files « File Input Output « Java






Get file size

   
import java.io.File;

public class Main {

  public static long getFileSize(String filename) {
    File file = new File(filename);

    if (!file.exists() || !file.isFile()) {
      System.out.println("File doesn\'t exist");
      return -1;
    }
    return file.length();
  }

  public static void main(String[] args) {

    long size = getFileSize("c:/my.txt");
    System.out.println("Filesize in bytes: " + size);
  }
}

   
    
    
  








Related examples in the same category

1.Create file
2.Create a temporary file
3.Creating a Temporary File and delete it on exit
4.Create a directory (or several directories)
5.Change last modified time of a file or directory
6.Construct file path
7.Create temporary file with specified extension suffix
8.Create temporary file in specified directory
9.Create new empty file
10.Compare two file paths
11.Delete a file
12.Delete file or directory
13.Deleting a Directory (an empty directory)
14.Delete file or directory when virtual machine terminates
15.Determine File or Directory
16.Determine if a file can be read
17.Determine if a file can be written
18.Determine if file or directory exists
19.Determine if File or Directory is hidden
20.Demonstrate File.
21.Moving a File or Directory to Another Directory
22.Find out the directoryFind out the directory
23.Get all path information from java.io.File
24.Getting an Absolute Filename Path from a Relative Filename Path
25.Getting an Absolute Filename Path from a Relative Filename with Path
26.Getting an Absolute Filename Path from a Relative Filename parent Path
27.Get Absolute path of the file
28.Get File size in bytes
29.Get parent directory as a File object
30.Get a file last modification date
31.File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
32.Getting the Parents of a Filename Path
33.Get the parents of an absolute filename path
34.Getting and Setting the Modification Time of a File or Directory
35.Mark file or directory Read Only
36.List Filesystem roots
37.List drives
38.Listing the Directory ContentsListing the Directory Contents
39.Rename file or directory
40.Forcing Updates to a File to the Disk
41.Random FileRandom File
42.Create a directory; all ancestor directories must exist
43.Create a directory; all non-existent ancestor directories are automatically created
44.Getting the Current Working Directory
45.Change a file attribute to writable
46.Data fileData file
47.Output to a text File
48.Choose a FileChoose a File
49.Read data from text file
50.Copy File
51.Querying a File for Information
52.Working with RandomAccessFileWorking with RandomAccessFile
53.Get a list of files, and check if any files are missing
54.Delete a file from within Java
55.Work with temporary files in Java
56.Compare File Dates
57.Sort files base on their last modified date
58.Strings -- extract printable strings from binary file
59.Get extension, path and file nameGet extension, path and file name
60.Read file contents to string using commons-io?
61.Get all xml files by file extension
62.Get icon for file type
63.Change a file attribute to read only
64.Get file extension name
65.Search for files recursively
66.Create a human-readable file size
67.Set file attributes.
68.Format file length in string
69.Return a file with the given filename creating the necessary directories if not present.