Results 1 to 4 of 4

Thread: FileWriter

  1. #1
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Rep Power

    Default FileWriter


    I am having a bit of difficulty understanding/Using FileWriter. Now by my understanding in the API FileWriter allows to use write at the end of your file, if you have text in there already, correct?

    I have a file with people info in it.
    and I created a GUI which allows people to input that data.

    Java Code:
    File file = new File("People.txt");
    FileWriter fw = new FileWriter(file);
    People p = new People(firstName, lastName, email, sex));
    Now, I keep getting an error on the fw.append(p) Can any one point me to right direction?

    Java Code:
    The method append(CharSequence) in the type Writer is not applicable for the arguments(People)

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Rep Power

    Default Re: FileWriter

    So you're trying to append a person object.

    Basically, the append function is wanting a sequence of characters or a string, and you're giving it p, which is a People, not a string.

    One solution:

    In your People class, add a function that converts the data to a string. Something like this:
    Java Code:
    //up here you have instance variables firstName, lastName, sex, and email which you initialize in your constructor
    public String toString() {
         return this.firstName + " " + this.lastName + ", " + + ", " +;
    Then when you want to append, simply call on that function for the instance so that the file writer has a useable data type (a string) to append to the file

    Java Code:

  3. #3
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Rep Power

    Default Re: FileWriter

    Thank you for reply AlexGral

    Yes, I have all my classes with a toString() method.

    Now, I have another question

    when you append the string, it means its going to "Print" my person object to the end of my "People.txt" file, correct?
    Because now, it deletes everything I had before in there. and by appending, i thought it would save it to the end.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: FileWriter

    The constructor call new FileWriter(file) is opening the file with overriding everything.
    Use new FileWriter(file, true) instead.

    Please read the documentation of FileWriter for all details.


Similar Threads

  1. FileWriter issue
    By victorgb in forum New To Java
    Replies: 4
    Last Post: 04-18-2012, 10:31 PM
  2. FileWriter error
    By kthomp271828 in forum Advanced Java
    Replies: 17
    Last Post: 11-29-2011, 07:45 PM
  3. FileWriter error
    By bczm8703 in forum JCreator
    Replies: 0
    Last Post: 03-04-2011, 05:10 AM
  4. New line in FileWriter
    By luc@$ in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 05:45 AM
  5. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 03:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts