• Post Reply Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile newLine in notepad  RSS feed

 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been struggling for sometime trying to get randomaccessfile class to write a newline. I know how to append the file using the length method, but it will just result in writing the code at the end of the file. I need a new line at the end of the file. I tried all the simple ways with no luck here is one way I tried


When I read the data back to me it's all on one line.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


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, I just need to know how to get it to write to next line.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


I missed the fact that you were including \n in your text. Don't do that. Let Java provide the system-dependent line terminator for you. If you use the proper classes and methods, such as PrintWriter.println() or BufferedWriter.newLine(), that will be done for you.
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


I doubt it. But what's tying you to Notepad, when better text editors are readily available?
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, let me try to explain my problem in more detail. I am asking a bunch of random questions and I am suppose to record the response the amount of times the person has tried and the amount the person got it correct. I am afraid to use other I/O classes because they delete preexisting data. So I decided to use RandomAccessFile class it works i thought. Here is the part of the code that is relevent


It prints the this on the end of my file " " weird characters that I cant get to correctly display on this screen. Very frustrated. Can someone help me out. I am new to coding. I started during the summer and now I am hitting a brickwall going 100 mph in a Ferrari.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



See my problem with the BufferedWriter Class is that use it does not delete my preexisting data, but I need to be able to rewrite in the same position. I want to read the variable and edit it and for that to happen it only should append the file once. The BufferedWriter class keeps apending my file, and if I set the constructor to false poof all my data is done. So I thought why not use the RandomAccessFile class.

My problem with RandomAccessFile is writing integers is a pain in the butt. It seems to only be capable of writing it in Byte form and that does me no good. So I am back to square one. I begin to see how something so simple can become so complex with Java. All I want to do is write a bloody integer at the eof one time and be able to continue to write at that location, and the BufferedWriter Class falls short and the RandomAccessFile class would work but it writes Integers.

I don't want to be a pest but I am almost finished with the program, but I am stuck and don't know where to turn for resources. So come to JavaRanch for guidance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Yes, I understand that my problem with using the BufferedWiter Class is the following:
It keeps appending the data at the end of file.
For example, if I have the class write 1 at the end of the file, and the next time it runs it will write 1 besides the previous 1. I need it to write over 1 and not append it anymore. Does that make sense?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your requirements are not clear to me.

Previously you said you need to append to the file. Now you're saying you need to overwrite it. So which is it?

Either way, if you're dealing with a line-based text file, BufferedWriter or PrintWriter wrapped around a FileWriter is the way to go.

Now, if you want to overwrite part of the file, but not the whole thing, then RAF might be what you want, but still probably not, unless all your lines are guaranteed to be the same length.

For instance, if you have


and you try to use RAF to overwrite the second line with "NEW LINE", you'll get something like this:


or, if you use RAF to overwrite the second line with "DIFFERENT LINE", you'll get something like this:


What you need to do in this case is to read the file in, then write it back out, replacing the relevant lines as you go.

So, please take a step back, and explain clearly and precisely what you're trying to accomplish. Otherwise it's impossible for anyone to help you.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. See I have a bunch of questions written in a text file. I need to record at the end of file how the user scores on the text file. After all the the questions I have a spot for the user score to be placed, so I can track his or her progress over time. It's suppose to be cumulative. I have tried the bufferWriter and it worked but the problem was it kept appending the data, I just needed it appended once single time and thereafter I need to rewrite in that location. Does that make more sense.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example:
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"2"

The number 2 should indicate that user got the question right two times, and if the program ran again it should show
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"3"

When i used the Bufferedwriter class it would write:
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"3","3","3"
The randomAccessFile seemed like a gem, because of the seek method. The problem I had with that was the whole byte problem when writing numbers.


And I don't know why the length matters. Thanks for helping me man. It's the last part of this program. I am not enrolled in the school, I just try to do the homework so I can learn Java by myself. I don't have a TA to help me.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamal Taylor wrote:
And I don't know why the length matters.


When you replace bytes with RAF, you're not saying "replace the current line with this new line." You're saying "replace exactly this many bytes with these new bytes, and leave all the other bytes in the file exactly as they are.

Go back and review my post with "First/Second/Third Line", "NEW LINE", etc. When you replace bytes in the middle of a file, if the new bytes aren't the same length as what you're replacing, you'll either have some of the original bytes left over, or the new ones will overwrite some of the stuff the came after the original bytes.

HOWEVER, if you're always replacing at the end of the file AND the new bytes are the same length or longer than the original, then it will work.

HOWEVER, I still wouldn't do that. I'd do the read-then-write approach I outlined above.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HOWEVER, I still wouldn't do that. I'd do the read-then-write approach I outlined above.


Thank you I will try the read then write approach. Give it a try, and hopefully it works.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Can you point me in the right direction how to read the data into the memory and edit like that. I am unsure on how to do this, but yet it sounds very intriguing and promising. Do you mean store the data in an Array and then edit the last index or did I miss something?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamal Taylor wrote:
Can you point me in the right direction how to read the data into the memory and edit like that.


Since you've figured out how to write a file, I assume you can figure out how to read one.

I would just store the lines in a List, ignore the last one, then write out each line in the List and then finally write out the new data for the last line.

Do you mean store the data in an Array and then edit the last index or did I miss something?


I'd go with a List rather than an array. But if you're not familiar with lists and not ready to learn them yet, and if you know how many lines there will be, an array can work too.
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, I am going to use the ArrayList and do as you said. It's 2:30 in the morning here so I will try it in the morning. Thanks for your help.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!