Writing at the EOF ( end of my file)
I am trying to become more independent in researching and finding solutions, but sometimes you hit a brick wall and it hurts. I was looking for a method to write at the end of the text inside the file. I looked on Oracle Documentation and it showed me this FileOutputStream and I normally use formatter method to print to a file. The constructor said that it would print at the end of file. Here it is: "Creates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. " So I was excited patted myself on my back a job well done got the code in, and what happens it deletes my info and writes over it. Luckily for me, I saved a back up of my text file just in case. Okay, I did not understand what they meant by " if the second argument is true", I only have one argument in my constructor so I am thinking did i do something wrong. Well, here is my code:
Thanks in advance
Re: Writing at the EOF ( end of my file)
...and what did the Oracle Documentation say about the use of the FileOutputStream class and:
public FileOutputStream(String name, boolean append)
Re: Writing at the EOF ( end of my file)
Alright, I set it to true and it worked. My last question, is now it writes it literally at the very end of the last sentence. Can I instruct it to write on the next line so it dose not look weird?
Re: Writing at the EOF ( end of my file)
Read the file, if it exists, and if there is not a blank line at the end, add a newline character first?
Re: Writing at the EOF ( end of my file)
Should I use the scanner class or BufferedReader class which would give me access to the readline method? Any tips or suggestions?
Re: Writing at the EOF ( end of my file)
Suggestion? Read the documentation for both classes and choose the one that fits your needs best.
Tips? Yea when it boils down in the end it really does not matter too much right now HOW* you do it, as long as it works. Remember back in grade school math class, when they show you the 'long' way to do things, and years later they show you the 'short' way to do the same thing? Well programming is the same way. They don't come out and tell you the short way first, they try to teach the background and how things work so that you can understand the short way when it is introduced.
Converse with others in your class (if you are in one). Converse with people on this site. You don't have to limit your posts to problems with a specific project or the code for the project. There is a java theory forum for discussing the art of programming in java. If you have a project and think you may be doing something the 'long' way, start a thread in the theory forum explaining the end goal, current code, and fire off discussion on the 'best' way to do it.
Be active in java and learning java will be a natural side effect.
*Yes it matters how you do what you do, in some situations one way is superior to the other. There is no way to tell you the best way every time. Experience and project requirements pretty much lay out what the code will be in the end. As you learn more about the language you will start seeing multiple ways to do the same thing and as you learn them, you will understand why to choose one way over the other. First try to write code that works, with help if needed, and when it is functional you then have a great candidate for the theory forum and improvements.
Re: Writing at the EOF ( end of my file)
Great, thank you for your well thought out post. I will be reading this many times as it provides great information.
I have solved the problem with appending my file, but of course it creates a new problem which was unseen before. The problem with the appending is that always appends. See, I have a certain text file that asks random questions. I check the progress of the user in a way that shows how many right he has and how many times it took him to get the answer. Being that the work is "cumulative" I need a static number that I can read in with the scanner class and store it. I don't need continuous numbers every time the program has been ran. I want to append the file one time and rewrite over it. If i use the formatter class, it erasers anything, and I don't know how to tell the FileOutputStream to only append once and then write in the specific area from now on. This is the last part of the program.
Re: Writing at the EOF ( end of my file)
2 different methods called at 2 different times for 2 different reasons to do 2 different things.
Before you were overwriting the file every time. Now you are appending every time. Adjust the code to overwrite when you want to overwrite and to append when you want to append.