Jamal Taylor wrote:
When I read the data back to me it's all on one line.
Jeff Verdegan wrote:
Jamal Taylor wrote:
When I read the data back to me it's all on one line.
Of course it is. If you read the docs for the methods you're using, you'll see that none of them says anything about writing newline characters.
Why are you using RAF anyway? If you just want to append text to a file, use the FileWriter constructor that takes an append argument. If this is just a text file, then use BufferedWriter or PrintWriter so you can write characters rather than bytes.
Paul Clapham wrote:It's also true that Notepad won't treat the \n (new-line) character as a new line. Officially the way to end a line of text in Windows is with the two characters \n\r (new-line, carriage-return) but Notepad is the only text editor that I know of which actually enforces that rule. Even Wordpad will treat \n as if it means the end of a line.
Jamal Taylor wrote:
The reason why I am using ReadAccessFile is if i use the BufferedWriter it deletes my preexisting data.
I am trying to append my file. ReadAccessFile class is amazing,
Jamal Taylor wrote:
Paul Clapham wrote:It's also true that Notepad won't treat the \n (new-line) character as a new line. Officially the way to end a line of text in Windows is with the two characters \n\r (new-line, carriage-return) but Notepad is the only text editor that I know of which actually enforces that rule. Even Wordpad will treat \n as if it means the end of a line.
Open the file in a different editor it works perfectly. Is there a way to get it to appear like that in notepad?
Thanks
Jamal Taylor wrote:Open the file in a different editor it works perfectly. Is there a way to get it to appear like that in notepad?
Jeff Verdegan wrote:
Jamal Taylor wrote:
The reason why I am using ReadAccessFile is if i use the BufferedWriter it deletes my preexisting data.
Then you didn't use new FileWriter(file, true).
I am trying to append my file. ReadAccessFile class is amazing,
It's also the wrong class for creating or appending to a line-based text file.
Jamal Taylor wrote:I am afraid to use other I/O classes because they delete preexisting data.
Can someone help me out.
Jeff Verdegan wrote:
Jamal Taylor wrote:I am afraid to use other I/O classes because they delete preexisting data.
Not if you use them correctly, as I suggested twice.
Can someone help me out.
I've been trying to, but you've been ignoring my advice. Use the proper class for the job. Since you're dealing with characters, not bytes, that class is a Writer. BufferedWriter or PrintWriter most likely, wrapped around a FileWriter.
Jamal Taylor wrote:Sorry, I will try to be more clear. I just want to append the end of file one time, and every time after that I would like to overwrite the data in the same exact spot.
Jamal Taylor wrote:
And I don't know why the length matters.
Jeff Verdegan wrote:
Jamal Taylor wrote:Sorry, I will try to be more clear. I just want to append the end of file one time, and every time after that I would like to overwrite the data in the same exact spot.
And will the new data be exactly the same length as the data it's overwriting? If so, RAF may be an option, but I wouldn't use it for a line-oriented text file anyway. (Do you understand why it matters if it's the same size?)
If I wanted to replace the last line in that text file, assuming it's not a terribly long file, I would read the whole file into memory, close the file, reopen it with a BufferedWriter or PrintWriter, and then write out the whole file, with the last line replaced by the new last line.
Jamal Taylor wrote:
Can you point me in the right direction how to read the data into the memory and edit like that.
Do you mean store the data in an Array and then edit the last index or did I miss something?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |