• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bufferedwriter/filewriter...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok in a constructor of mine,

I initialize a global bufferedwriter object out = new BufferedWriter(new
FileWriter(filename));


then in an adduser method...

i try{
out.write()
}


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???

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my source code for the above question...

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

help

Justin
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

anyways..

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

i didnt see a method in the API...

Justin
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic