Name: skT88420 Date: 11/12/99
java version "1.2"
Classic VM (build JDK-1.2-V, native threads)
The java.io.File.length() method does not reflect the actual file size.
There is a distinct latency between writing to a file and
being able to get the resulting size.
The code below illustrates the problem, showing 20 lines ending in:
before write mod time: 942403265251 size=0 bytes written=720
after write mod time: 942403265251 size=0 bytes written=800
If the Thread.sleep is uncommented, the size= correctly follows the bytes
written value.
My first thought was that this is due to some NTFS latency, but the Perl script
at
the end does the same thing but sees the correct file size after each syswrite.
Is there any more low-level flush or sync method I need to call to get
an accurate file size?
++++ start FileSize.java ++++
import java.io.*;
public class FileSize
{
final static String outFileName = "C:/temp/FileSize.tmp";
final static byte[] data = "123456789 123456789 123456789 123456789
123456789 123456789 123456789 123456789\n".getBytes();
final static int dataLen = data.length;
static File outFile = null;
static int bytesWritten = 0;
static public void main(String args[])
{
outFile = new File(outFileName);
FileOutputStream fo;
outFile.delete(); // Remove any previous output file.
try
{
fo = new FileOutputStream(outFile);
for (int i=0; i < 10; i++)
{
showFileInfo("before write ");
fo.write(data);
fo.flush();
// Thread.sleep(1000L);
bytesWritten += dataLen;
showFileInfo(" after write ");
}
} catch (Exception e)
{
System.out.println("Exception - " + e);
}
}
static void showFileInfo(String prefix)
{
System.out.println(prefix + "mod time: " + outFile.lastModified()
+ " size=" + outFile.length() + " bytes written="
+ bytesWritten);
}
}
++++ end FileSize.java ++++
++++ start fsize.pl ++++
#!perl -w
$OUT_FILE = "C:/temp/fsize.tmp";
open(OUT_FILE, ">$OUT_FILE") || die "Can't create $OUT_FILE - $!\n";
$DATA = "123456789 123456789 123456789 123456789 123456789 123456789 123456789
123456789\n";
$DATA_LEN = length($DATA);
$bytesWritten = 0;
for ($i=0; $i < 10; $i++)
{
showFileInfo("before write ");
my $bytes = syswrite OUT_FILE, $DATA, $DATA_LEN;
print "Wrote $bytes, not $DATA_LEN!\n" if ($bytes != $DATA_LEN);
$bytesWritten += $DATA_LEN + 1; # +1 for additional "\r" added in
non-binmode
showFileInfo(" after write ");
}
exit 0;
sub showFileInfo()
{
my ($prefix) = @_;
unless ( ($SIZE,undef,$MTIME) = (stat(OUT_FILE))[7..9] ) {
warn ("Can't stat $OUT_FILE: $!\n");
return 0;
}
print $prefix . "mod time: " . localtime($MTIME)
. " size=" . $SIZE . " bytes written="
. $bytesWritten . "\n";
}
++++ end fsize.pl ++++
(Review ID: 97766)
======================================================================
- duplicates
-
JDK-4863147 (NT only) File.length() returns incorrect length
-
- Closed
-