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
Solved

Not all items in array are outputting to file.

Posted on 2004-03-31
10
236 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?
0
Comment
Question by:Ravi Singh
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 10724272
Best to use a PrintWriter

File f = new File("javadir");
PrintWriter out = new PrintWriter(new FileWriter("file.txt"));
String[] fileNames = f.list();
for (int i = 0; i < fileNames.length; i++) {
      out.println(fileNames[i]);
}
out.close()
0
 
LVL 18

Author Comment

by:Ravi Singh
ID: 10724310
i instantiated  it like this:

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

could it be the bufferedwriter then?
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 25 total points
ID: 10724343
>> could it be the bufferedwriter then?
Could be.
A buffered writer has a flush() call. Use it before closing
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 37

Expert Comment

by:zzynx
ID: 10724363
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.
0
 
LVL 18

Author Comment

by:Ravi Singh
ID: 10724382
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10724386
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()
0
 
LVL 18

Author Comment

by:Ravi Singh
ID: 10724389
Thanks zzynx.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10724410
You're welcome.
Thanks for accepting. That keeps us going.
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10724447
8-)

Not much point in flushing btw at that point. The buffered will automatically be flushed on close
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10724544
>> The buffered will automatically be flushed on close
Good to know. Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 41
windows explorer path to command prompt 5 54
tomcat administrtor 12 61
use lov values 2 61
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

860 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