Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File.length() problem

Posted on 2002-04-16
6
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 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