File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Writing to a File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Writing to a File" Watch "Writing to a File" New topic
Author

Writing to a File

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi guys, I am taking an input from a file whose contents are 1,4,2,5,6,4,7 etc.. and am sorting it. Everything works fine but I am unable to write the output back to another file. Basically.. I am unable to write anything back to a file. File Numbers2.txt is getting created tho.

Am pasting the code below. Am not gettin any compilation error.

i

Can anybody tell me where am doing wrong

Thanks.
[ July 01, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
Mark Freeman
Ranch Hand

Joined: Aug 02, 2006
Posts: 40
It looks like you need to flush the output and close the file. After your loop code block containing this line:



add



it wouldn't be bad to close() reader either.
[ July 01, 2008: Message edited by: Mark Freeman ]
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks Mark it works now but I am gettin some unnecessary characters in my ouput file. The output am gettin is 2 4556677890


I changed the code to



Can you tell me why?

Thanks.
Mark Freeman
Ranch Hand

Joined: Aug 02, 2006
Posts: 40
This may not make a difference (I am not where I can test it), however, have you tried flush() outside of the loop? If I understand the purpose of flush() correctly, it flushes the buffer. I would want to do this just once, after I am finished writing everthing out. It seems worth a try.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi Mark,

Thanks for ya reply. Um.. I tried doing that but am still getting the illegal characters between the numbers in my output. It's nothing imp but I was just wondering what's causing this.

Thanks.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi Mark,

I figured out what was going wrong. I had after this statement writer.write(arr[i]), this one writer.write(i); I put it there to test and did not comment it later on. Now, I've commented it and the code's working fine and am able to see the correct output. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to a File
 
Similar Threads
trouble trying to read a file and copy it to another....
how to see how control is moving in a java program during execution
Unix Time Conversion
write reversed lines to a file
what is wrong with my code?.