Solved

File.length() problem

Posted on 2002-04-16
6
262 Views
Last Modified: 2010-03-31
FileOutputStream out = new FileOutputStream("c:/test");
out.write("some data".getBytes());
out.flush(); out.close();
File f = new File("c:/test");
System.out.println("File size is: " + f.length());

Sometimes the outputs is:
File size is: 0


pls, help

0
Comment
Question by:dmge
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6944803
And the writing take place or not ?
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 250 total points
ID: 6944806
Hi dmge,

I had the same problem. This is because of flush() method desn't call the OS's flush(). To prevent from getting incorrect file size, use this:

FileOutputStream out = new FileOutputStream("c:/test");
out.write("some data".getBytes());
out.flush();
// OS level flush !!!
out.getFD().sync();
out.close();

File f = new File("c:/test");
System.out.println("File size is: " + f.length());



0
 
LVL 9

Expert Comment

by:Ovi
ID: 6944842
I have no problem using this code :

import java.io.*;
import java.util.*;
import java.text.*;

public class FileWrite {
  public static void main(String[] args) {
    for(int i = 0; i<1000; i++) {
      File f = new File(System.getProperty("user.dir") + File.separator + "test.txt");
      try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write("Test writing to file");
        bw.flush();
        bw.close();
      } catch(Exception e) { e.printStackTrace(); }
      System.out.println(i + " File : " + f.getAbsolutePath() + "; size : " + f.length());
    }
  }
}


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:Venci75
ID: 6944875
Here you can find more information about this:
http://developer.java.sun.com/developer/bugParade/bugs/4290946.html
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6945030
This bug was reported against JDK 1.2.x, now there is JDK 1.4.
0
 

Author Comment

by:dmge
ID: 6945100
This works!
You are right Ovi, but I have a requirement to be java 1.2 compatible.

Thanks for your help
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question