Jump to Real's How-to Main page

Convert bytes to megabytes

import java .io.*;

public class Test {

 public static void main(String args[]) {
   // a big file ...
   File f = new File("news.easynews.com.newsrc");
   System.out.println(f.length());
   System.out.println(bytesToMeg(f.length()) + " Mb");
 }

 private static final long  MEGABYTE = 1024L * 1024L;

 public static long bytesToMeg(long bytes) {
  return bytes / MEGABYTE ;
 }
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2006
[ home ]