• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileWriter wait to finish

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roberto Gonsales wrote:Thanks for the answer Campbell. . . . Thanks

You're welcome
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

And I have no problem being called by my last name; it brings back memories of school.
 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic