1

I am trying to get the length (in bytes) of a file, but every time I test different files, the result is invariably 4065123.0.

This is the relevant portion of my code:

File file = new File(path + filename);
if(!file.exists()) {
    System.out.println("File does not exist");
} else {
    double bytes = file.length();
}

How do I correctly get the length of a file?

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
CoderT
  • 182
  • 1
  • 12
  • 1
    Post a reproducible, complete example. And why the hell are you using a double? You think you have some half of bytes in your files? – JB Nizet Nov 02 '14 at 13:10
  • First, why are you storing a `long` as a `double`? Second, this code looks correct, except I can't see where you are printing the length - I suspect the problem is there. – Boris the Spider Nov 02 '14 at 13:11
  • This is how you do it. If this is not working when you call it repeatedly, you are doing something else wrong. – Peter Lawrey Nov 02 '14 at 13:14
  • Just search on google and you find a duplicate question with a perfectly good answer ([this](http://stackoverflow.com/questions/2149785/get-size-of-folder-or-file)). – bcsb1001 Nov 02 '14 at 13:16
  • the length is printed in client side. – CoderT Nov 02 '14 at 13:16
  • 1
    Try looking in a debugger. Sounds as if the number is not actually getting to 'the client side'. – bmargulies Nov 02 '14 at 13:23

4 Answers4

1

Try:

    File file =new File("myfile_in_test.java");

    if(file.exists()){

        final double bytes = file.length();
        final double kilobytes = (bytes / 1024);

        System.out.println("bytes : " + bytes);
        System.out.println("kilobytes : " + kilobytes);
    }else{
         System.out.println("File does not exists!");
    }
SMA
  • 36,381
  • 8
  • 49
  • 73
0

Here is an example which shows that it works just fine.

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        File file = File.createTempFile("deleteme", ".txt");
        file.deleteOnExit();
        Thread main = Thread.currentThread();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    do {
                        Thread.sleep(200);
                        long length = file.length();
                        System.out.println("File " + file + " is " + length + " bytes long.");
                    } while(main.isAlive());
                    System.out.println("Finished");
                } catch (InterruptedException e) {
                    System.err.println("Interrupted");
                }
            }
        }, "monitor");
        t.start();

        FileOutputStream fos = new FileOutputStream(file);
        for(int i=0;i<2000;i++) {
            fos.write("words words words words words words words words words words words\n".getBytes());
            Thread.sleep(1);
        }
        fos.close();
    }
}

prints something like.

File /tmp/deleteme4214599935706768614.txt is 11880 bytes long.
File /tmp/deleteme4214599935706768614.txt is 23562 bytes long.
File /tmp/deleteme4214599935706768614.txt is 35376 bytes long.
File /tmp/deleteme4214599935706768614.txt is 47256 bytes long.
File /tmp/deleteme4214599935706768614.txt is 59136 bytes long.
File /tmp/deleteme4214599935706768614.txt is 70950 bytes long.
File /tmp/deleteme4214599935706768614.txt is 82830 bytes long.
File /tmp/deleteme4214599935706768614.txt is 94644 bytes long.
File /tmp/deleteme4214599935706768614.txt is 106524 bytes long.
File /tmp/deleteme4214599935706768614.txt is 118338 bytes long.
File /tmp/deleteme4214599935706768614.txt is 130218 bytes long.
File /tmp/deleteme4214599935706768614.txt is 132000 bytes long.
Finished
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-1

Try using a long value:

long byt = file.length();
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • I believe `Double.MAX_VALUE > Long.MAX_VALUE` so this shouldn't be a problem. – Boris the Spider Nov 02 '14 at 13:12
  • 1
    @BoristheSpider For very large files (possibly larger than those supported by the OS) you can lose precision. – Peter Lawrey Nov 02 '14 at 13:13
  • 1
    I have used long and the result is 4705984196517036032, and this result is repeated for any file. – CoderT Nov 02 '14 at 13:15
  • @PeterLawrey yes, true. But I don't think it would always return the same value - unless we're in some really degenerate case. – Boris the Spider Nov 02 '14 at 13:15
  • @NewStar That would sense if you had a 4,705,984 TB file as you lose some precision, however it is far more like you don't have NSA sized disk space in one file and you have an error in your system. The file shouldn't appear to be that large. – Peter Lawrey Nov 02 '14 at 13:25
-1

First of all byt must be long. Another thing may happen if you ask length of a directory of device. For regular file it is the correct way, just change your variable to correct type.

Dmytro
  • 496
  • 7
  • 17