Solved

File.length() problem

Posted on 2002-04-16
6
253 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
Comment Utility
And the writing take place or not ?
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 250 total points
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
This bug was reported against JDK 1.2.x, now there is JDK 1.4.
0
 

Author Comment

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

Thanks for your help
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now