Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File.length() problem

Posted on 2002-04-16
6
Medium Priority
?
281 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

971 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