7 Replies Latest reply: Mar 29, 2011 6:47 AM by EJP RSS

    Problem in replacing the String in a Fille.

    boopathi
      I need to replace the String in the text file, am able to do it partially.
      I mean, am able to replace it. But there are some discrepancies if the replacement string is of "less" or "more" in length than the existing one.

      I solved it for the replacement string of less size, but am unable to do it for the string of more size.. :(

      The problem am facing is "when the replacement string is larger than the existing one, then the other subsequent line in the file gets overwritten".

      I have used RandomAccessFile class for reading and writing the same file.
      fpointer1 = randomFile.getFilePointer() - strLine.length() - 2;
      strLine1 = strLine;
      int sInd = strLine1.indexOf('\'');
      int eInd = strLine1.indexOf(';');
      
      String oldStr = strLine1.substring(sInd + 1, eInd - 1);
      strLine1 = StringUtils.replace(strLine1, oldStr, newStr);
      if (oldStr.length() < newStr.length()) {
      int diff = (newStr.length() - oldStr.length()) + 1;
      randomFile.writeBytes("\r\n");
      for (int i = 0; i < diff; i++) {
      randomFile.writeBytes(" ");
      }
      randomFile.seek(fpointer1);
      randomFile.writeBytes(strLine1);
      
      } else if (oldStr.length() > newStr.length()) {
      randomFile.seek(fpointer1);
      randomFile.writeBytes(strLine1);
      
      int diff = (oldStr.length() - newStr.length()) + 1;
      for (int i = 0; i < diff - 1; i++) {
      randomFile.writeBytes(" ");
      }
      randomFile.writeBytes("\r\n");
      }
      break;
      }
      Can u pls help me in resolving this issue???...
        • 1. Re: Problem in replacing the String in a Fille.
          TPD-Opitz
          boopathi wrote:
          I need to replace the String in the text file, am able to do it partially.
          Don't do it on the file level!

          - Create a BufferedReader for the file
          - read the file line by line and make your changes using string.replaceAll("PATTERN","newText")
          - write the changed lines to a temorary file using a (Buffered) FileWriter
          - remove the original file
          - rename the temp file to the name of the original

          bye
          TPD
          • 2. Re: Problem in replacing the String in a Fille.
            Kayaman
            boopathi wrote:
            I have used RandomAccessFile class for reading and writing the same file.
            That's what causes the problem. You can't append bytes without overwriting the ones on the next line.

            What you need to do is read the file line by line writing out as you go. When you find the line you want to modify, do the modification and then write the modified line out and all the lines after that.

            You can use a temp file where you write the modified version and when it's done, replace the original with the temp file.
            • 3. Re: Problem in replacing the String in a Fille.
              boopathi
              Hi,

              Thanx for ur rly..

              Ur suggestion is helpful, but I am reading the big file and the replacement will be done within first 10 lines. Thats why I didnt preferred the method which you suggested.

              Is it possible to do using the RandomAccessFile class or not?? can you answer this one...
              • 4. Re: Problem in replacing the String in a Fille.
                TPD-Opitz
                boopathi wrote:
                [...]I am reading the big file and the replacement will be done within first 10 lines.
                No!
                As soon as you change the length of the replaced sequence you have to move all following byte in the file to their new location. There is no other way than to touch each byte individually.

                bye
                TPD
                • 5. Re: Problem in replacing the String in a Fille.
                  boopathi
                  Hi,

                  Thanx for ur rly!!!...

                  As u said the overwriting causes the problem, but i managed to resolve for the string of less size. So thats why am seeking to manage the larger size string to replace without overwriting the sub sequent lines.

                  Can you help me in suggesting whether it can be resolved using the getFilePointer() and seek() methods??...
                  • 6. Re: Problem in replacing the String in a Fille.
                    Kayaman
                    boopathi wrote:
                    As u said the overwriting causes the problem, but i managed to resolve for the string of less size.
                    I assume you did this with some padding? That's not exactly a clean solution.
                    So thats why am seeking to manage the larger size string to replace without overwriting the sub sequent lines.

                    Can you help me in suggesting whether it can be resolved using the getFilePointer() and seek() methods??...
                    No you can't.

                    Whether you shorten or lengthen the position where you modify the file, you need to move the remaining bytes back or forward, depending which is the case.

                    You managed to solve the first case by printing out padding, so you don't need to move the bytes back, but you can't solve the other case like this. You can lengthen the file and move the remaining bytes forward, but I probably wouldn't bother since a simple Reader-based approach is a lot better and easier to get right.
                    • 7. Re: Problem in replacing the String in a Fille.
                      EJP
                      Can you help me in suggesting whether it can be resolved using the getFilePointer() and seek() methods??...
                      It can't be resolved at all. The only way you can 'resolve' it for the case where the replacement is shorter is to write junk of some kind, maybe nulls or spaces. That's not a proper solution and there isn't one for the opposite case.

                      Review the requirement. You almost certainly need a database.