Append to exsisting text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave W

    Append to exsisting text file

    Hi,

    I'm hoping for some guideance. I'm trying append to the bottom of a text
    file using Java.

    Any advice, further reading would be greatly appreciated!

    Cheers,
    Dave


  • Luca Paganelli

    #2
    Re: Append to exsisting text file

    See

    public PrintWriter(Wri ter out,
    boolean autoFlush)

    --
    Luca Paganelli
    ICQ# 52629494


    Comment

    • Fahd Shariff

      #3
      Re: Append to exsisting text file

      See

      FileWriter
      public FileWriter(Stri ng fileName,
      boolean append)
      throws IOException

      Constructs a FileWriter object given a file name with a boolean
      indicating whether or not to append the data written.

      Sample code:

      BufferedWriter writer = new BufferedWriter(
      new FileWriter("myf ile.txt",true)) ;
      writer.write("H ELLO") ;
      writer.close() ;

      Fahd Shariff

      "Let the code do the talking..."

      Comment

      • Dave W

        #4
        Re: Append to exsisting text file

        Thanks for that, but i'm hoping you can help me with my syntax. It
        currently makes a new text file/replaces but i'm unsure how to append an
        already exsisting one.

        This is what i have:

        try
        {
        String outName = "out.txt";
        FileWriter out = new FileWriter(outN ame);
        writer.write(to tal);
        writer.newLine( );
        writer.close();
        }

        Thanks,
        Dave

        "Fahd Shariff" <fahdshariff@ya hoo.com> wrote in message
        news:9bc0209f.0 405120114.1e0eb e5c@posting.goo gle.com...[color=blue]
        > See
        >
        > FileWriter
        > public FileWriter(Stri ng fileName,
        > boolean append)
        > throws IOException
        >
        > Constructs a FileWriter object given a file name with a boolean
        > indicating whether or not to append the data written.
        >
        > Sample code:
        >
        > BufferedWriter writer = new BufferedWriter(
        > new FileWriter("myf ile.txt",true)) ;
        > writer.write("H ELLO") ;
        > writer.close() ;
        >
        > Fahd Shariff
        > http://www.fahdshariff.cjb.net
        > "Let the code do the talking..."[/color]


        Comment

        • Dave W

          #5
          Re: Append to exsisting text file

          Sorry, let me correct that:

          try
          {
          //ask user for file name to write to

          FileWriter out = new FileWriter("out .txt");
          BufferedWriter writer = new BufferedWriter( out);
          writer.write(to tal);
          writer.close();
          }


          Comment

          • Fahd Shariff

            #6
            Re: Append to exsisting text file

            Change to :
            FileWriter out = new FileWriter("out .txt", true);

            If the file does not exist it is created and if it does, text is appended to it.

            Fahd Shariff

            Comment

            Working...