We help IT Professionals succeed at work.

Not all items in array are outputting to file.

Ravi Singh
Ravi Singh asked
on
Medium Priority
276 Views
Last Modified: 2010-03-31
Hi i recently asked the question on how i could read files in from a directory which i specify. What has been done is...

The file names have been read from a directory and stored in an array of type string. I then iterate through the array using a for loop to print each file name in the array to an external "txt" file. But when i open the txt file only about 180 file names have been written to it when in fact the array holds around 390 file names. ( which i checked using the static variable length in array)

Why doesnt all the file names output to the external file?

Note: For outputting to an external file i am using a java.io.FilerWriter. Could it be this?

Initially i thought it could be because i am using an array, so instead i used another collection called the treeset, but this has the same effect to the program.

Anyone know why, and how i may resolve this?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Ravi SinghSenior Software Engineer

Author

Commented:
i instantiated  it like this:

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(newFileName)));

could it be the bufferedwriter then?
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
btw: It was a good idea to use a buffered writer.
I quote from the java help of BufferedWriter:

In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, ****it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters****. For example,

 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
 
will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
Ravi SinghSenior Software Engineer

Author

Commented:
Ah sorry ive done this so many times and still havnt learned, I didnt close the printwriter i just noticed it now from your code CEHJ. Sorry to bother you.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
So in CEHJ's example it would be:

File f = new File("javadir");
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
PrintWriter out = new PrintWriter(bw);
String[] fileNames = f.list();
for (int i = 0; i < fileNames.length; i++) {
      out.println(fileNames[i]);
}
bw.flush();
out.close()
Ravi SinghSenior Software Engineer

Author

Commented:
Thanks zzynx.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
You're welcome.
Thanks for accepting. That keeps us going.
:-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)

Not much point in flushing btw at that point. The buffered will automatically be flushed on close
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> The buffered will automatically be flushed on close
Good to know. Thanks.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.