Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

writing to a text file

A few days back I asked how to read from a text file, got lots of help. thanks.

NOW i want to know how to write to a text file
here's the situation

I've got a client and server.
what ever I input into the client goes to the server and the server writes that to a file.txt

would appretiate it if someone could help me with this.

Thanks in advance.
Rohan.
[411 byte] By [rohan076] at [2007-11-11 8:17:32]
# 1 Re: writing to a text file
Hi,

You can do something like this:

FileOutputStream fos = new FileOutputStream("file.txt");
PrintWriter r = new PrintWriter(fos);

// when something comes in from client
r.println("....");

// when finished with file
fos.close();

Kind regards,
Noel
noelob at 2007-11-11 22:35:58 >
# 2 Re: writing to a text file
Thanks Noel, it worked perfect
rohan076 at 2007-11-11 22:36:57 >
# 3 Re: writing to a text file
// when finished with file
fos.close();

you also should use fos.flush(); to enshure all contents of the stream have been flushed, before cosing. on some systems this is mandatory.

fos.flush();
fos.close();
graviton at 2007-11-11 22:37:50 >