wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes bufferedwriter/filewriter... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "bufferedwriter/filewriter..." Watch "bufferedwriter/filewriter..." New topic


Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok in a constructor of mine,

I initialize a global bufferedwriter object out = new BufferedWriter(new

then in an adduser method...

i try{

now i catch the exception, but evidently one is never thrown,

and it creates the file with the name of the string in filname variable..

so... well atually filname is a File object.

can anyone tell me why it is not working???


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
here is my source code for the above question...

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, I tested your code, and it did not throw any exception. The only problem I found is that you are not neither closing the stream nor flushing it, and that is why the Strings are not written into the file.

You must flush the out stream after writing the user or close the stream for all its data to be flushed into the file, or it will be kept in a temporary buffer until it is full.

Maybe you can print a strack trace when you catch the exception and tell us why you are having trouble with the file. Maybe you do not have reading or writing privileges on the operating system or another IO reason.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
As Edwin noticed you did not flush the BufferedWriter's buffer. There is an obvious method for doing that

Also, you should consider how you are comparing strings when you try to find a user record. "==" does not do the job at all well.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok ok : ), i fixed the flush, and it works, and i used out.newLine() instead of writing "\n".

and for the '==' I put log.equals(login) etc..

but, now i have a bigger problem..

each time i run the frame, it creates a new instance of DataBase,
and at first i thought the one = new bufferedwriter...

would just write to the same file that already existed, but it
created a new file, and overwrote my logins and such..

i tried

but then it didnt create an instance of bufferedwriter,
and when i tried you register a user, i got a null
pointer exception, because

BufferWriter one;

wasn't initialize...


Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The Writer has another overloaded constructor:The 2nd boolean parameter indicates whether to append to file or not. If true, then stuff will just be appended if the file already exists, otherwise the file will just be overwritten.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
k, that sounds good.

ummm, i created a BufferedReader, instead of the scanner.

because the:

BufferedWriter out = new BufferedWriter(new FileWriter("database.txt",true);

just opens the file that already exists...

buf if i used a file object instead of "database.txt"

i think that was overwriting it too


how do you go to the next line with a BufferedReader...

i didnt see a method in the API...

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: bufferedwriter/filewriter...
Similar Threads
Need help writing a text file
what is wrong with this code?
Opening files in append mode
write a int to a file
Question about File, FileWriter + BufferedWriter