permaculture playing cards*
The moose likes Beginning Java and the fly likes FileWriter wait to finish Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileWriter wait to finish" Watch "FileWriter wait to finish" New topic
Author

FileWriter wait to finish

Roberto Gonsales
Greenhorn

Joined: Sep 26, 2011
Posts: 5
Hi, I'm newbie to java.
My problem is I cant make the filewriter to finish the job, all files are written unfinished.

Here is my one class programm



The problem is the FileWriter writes chunks of 4kb size as it is in BufferedWriter. As you can see, write to file function is called on every end of the for loop. I guess next loop begins before the writing job is finished, that's why it remains unfinished.
Is the problem clear? Help me please.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Welcome to the Ranch
Why have you made all your methods static?
Why are you removing duplicates from the List? Why don't you simply create a Set from that List? You do realise that your means of removing duplicates will remove all ordering from your List, because HashSet supports neither ordering nor sorting?
I think it would be a good idea to use a different identifier from out, eg fileOut, to avoid confusion with System.out.

Why have you written out.? Is that supposed to be short for out.close()? You realise you have out.[close()] inside the loop. That would mean you close your writer after the first line... That may be the cause of your problem, and moving that line outside the loop might sort it.
Roberto Gonsales
Greenhorn

Joined: Sep 26, 2011
Posts: 5
Thanks for the answer Campbell. You see, I have never coded java. English is not my native language, but I want to grow up. That's why I write a program in java and post the question on this forum.
This program is not for anybody, this is only for my personal purposes, for my own project.
I realize that I'm doing wrong many things, but if I wont do anything because of fear of mistake I'll never learn anything.

So I need to finish this to make my progress. And now the problem is that filewriter writes buch of files multiple of 8kb by size. If data was less than 8kb the file is empty. I need it to write all the data from the list to the file. I swear that I'll fix all the wrong things later when there will be need of more than just this piece of work.
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Roberto Gonsales wrote:Thanks for the answer Campbell. . . . Thanks
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Roberto Gonsales wrote:. . . I have never coded java.
So who wrote that code? It looks like code written by somebody who has coded Java™ before.
. . . And now the problem is that filewriter writes buch of files multiple of 8kb by size. If data was less than 8kb the file is empty. . . .
I have never heard of a FileWriter behaving like that. Apart from the close() instruction (which should cause an Exception if you try to write to a closed Writer), I can see nothing wrong with your writing code.
I would suggest you print out the line you are writing to the file, inside that loop, withThen you can verify that all the lines expected are being written, comparing the size of the output to the size of the file. Put the close() instruction after the loop. Because of the buffering, the Writer does not guarantee to write everything until it is flushed. You must close the Writer to avoid a resource leak, so you must call close(), preferably in a finally block. If you call close(), that calls flush(), so there is no need to do both. But if you fail to call close() or flush(), the buffer may remain full for ever. That might be the problem.

If that doesn't work, get a printout of what you are writing into that List, to confirm you are reading the input file completely.
Roberto Gonsales
Greenhorn

Joined: Sep 26, 2011
Posts: 5
Thanks Ritchie, I called you by last name previous time I guess
I used to code action script. The syntax is pretty same, but not the api. I think I could make this in flex and it would be easier for me, but AS is not fast enough as Java.
I put all the functions to new class object to avoid using static. The problem is solved with your advise Ritchie.
I've added after for loop:

And this works. Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Well done

And I have no problem being called by my last name; it brings back memories of school.
 
 
subject: FileWriter wait to finish
 
Similar Threads
StringBuffer to File - Need Example
getting Class cast exception
write to f file
DOM doc is not adding a ProcessingInstruction
How to encode image in parts?