Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Writing at the EOF ( end of my file)

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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:
     FileOutputStream fm = new FileOutputStream(f);
     
     
          String number = Integer.toString(IOquiz.Question1PlayerResponse);
           byte[]contentInBytes = number.getBytes();
           fm.write(contentInBytes);
    			fm.flush();
    			fm.close();
           }
           catch(Exception Ex){
               System.out.print("Trouble in China");
           }

    Thanks in advance


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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)

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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?

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default 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.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

Similar Threads

  1. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  2. End of file indication
    By moonah in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 2nd, 2012, 09:46 AM
  3. Read file until EOF from offset each time
    By Pr0ject-Rec0n in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 2nd, 2010, 09:13 PM
  4. end of file \ file offset
    By JayRay in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: March 27th, 2010, 04:11 PM
  5. How can I detect the end of a line in a file ?
    By lumpy in forum Java Theory & Questions
    Replies: 3
    Last Post: February 18th, 2010, 03:13 AM