Create a human-readable file size : Files « File Input Output « Java






Create a human-readable file size

   
import org.apache.commons.io.FileUtils;

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File file = new File("c:\\java.exe");

    long size = file.length();
    String display = FileUtils.byteCountToDisplaySize(size);

    System.out.println("Name    = " + file.getName());
    System.out.println("size    = " + size);
    System.out.println("Display = " + display);
  }
}

   
    
    
  








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