7 Replies Latest reply: Mar 22, 2009 2:48 PM by 807588 RSS

    How do I edit a text file in Java?

    807588
      Hi everyone,

      I'd like to open a text file and edit the text, that is to change and edit a text file, that already has been written, such as add text in the middle or at the begining of the text file.

      I used RandomAccessFile class and seek method to work with text files, but I connot use it to add text to the middle of the file or to the begining of an already written text file.

      Is there a way in Java to add text to the begining or the middle of a text file?

      Thank you,
        • 1. Re: How do I edit a text file in Java?
          807588
          thunderball1234 wrote:
          Is there a way in Java to add text to the begining or the middle of a text file?
          No! Nor is there a way to do it in C++, C#, PASCAL, FORTRAN etc etc etc.
          • 2. Re: How do I edit a text file in Java?
            807588
            rewrite the file.

            Think of how files are stored on a hard-drive. You can't just add a random bit of info to the middle of a textfile without risking overwriting something else that you didn't mean to overwrite.
            • 3. Re: How do I edit a text file in Java?
              807588
              sabre150 wrote:
              No! Nor is there a way to do it in C++, C#, PASCAL, FORTRAN etc etc etc.
              dâmn! this bug is EVERYWHERE!
              • 4. Re: How do I edit a text file in Java?
                JosAH
                sabre150 wrote:
                thunderball1234 wrote:
                Is there a way in Java to add text to the begining or the middle of a text file?
                No! Nor is there a way to do it in C++, C#, PASCAL, FORTRAN etc etc etc.
                On the ARM/OS filing system you can prepend bytes to the beginning of the file ... that was fun.

                kind regards,

                Jos
                • 5. Re: How do I edit a text file in Java?
                  807588
                  JosAH wrote:
                  sabre150 wrote:
                  thunderball1234 wrote:
                  Is there a way in Java to add text to the begining or the middle of a text file?
                  No! Nor is there a way to do it in C++, C#, PASCAL, FORTRAN etc etc etc.
                  On the ARM/OS filing system you can prepend bytes to the beginning of the file ... that was fun.
                  :-) I'm glad to say that I missed out on the ARM/OS.
                  • 6. Re: How do I edit a text file in Java?
                    807588
                    Encephalopathic wrote:
                    sabre150 wrote:
                    No! Nor is there a way to do it in C++, C#, PASCAL, FORTRAN etc etc etc.
                    dâmn! this bug is EVERYWHERE!
                    Not everywhere.
                    On Tandem NSK (then Compaq NSK, then HP Compaq NSK)
                    Guardian text files (file code 101) are not simple ascii text files
                    but blocks containing a linked list of lines.
                    The native editors and any program that wants to read or manipulate text files
                    use the special text file API provided.
                    You can read/manipulate Guardian text files from C or TAL, probably also any other programming language supported.
                    • 7. Re: How do I edit a text file in Java?
                      807588
                      Thank you everybody!

                      Edited by: thunderball1234 on Mar 22, 2009 12:47 PM

                      Edited by: thunderball1234 on Mar 22, 2009 12:48 PM