Web Software Architecture and Engineering – Life on the Bleeding Edge

As I deal with the mail server issue (see previous post), I was thinking how to automate some of the find and replace I am doing.
I know that CF8 has many new functions and memory enhancements when dealing with files, but I was wondering if anyone has a UDF or CFC dealing with the following use case.
1. Open target text file
2. Replace text on line number X
3. Save target text file
Sound simple, right? But I have not found any code in ColdFusion, where you can explicitly target a line number inside a text file for some action. Has anyone seen this, or something similar in JAVA which could be turned into a UDF or CFC?
 

Advertisements

Comments on: "Help! Reading Text Files Through Line Number" (17)

  1. Jim Connor said:

    The following line converts a text file into an array of lines, making it easy to work with a specific line number:

  2. Jim,

    Yes, I considered something similar, however moving all the lines into array and storing in memory would be expensive… CF8’s newer function can work line by line, and I don’t know if there is a solution using them or not, but performance definitely is a consideration, as I may have to loop through hundreds of files.

  3. Hmm. So I know that Java lets you seek to a position in a file and read in bits “mid file.” I’m not so sure if you _write_ only a few bits in the middle of the file.

    Oh dude… this looks perfect (warning, didn’t read TOO carefully): http://stackoverflow.com/questions/181408/best-way-to-write-bytes-in-the-middle-of-a-file-in-java

  4. Ray,

    Thanks for the suggestion. Unfortunately this only adds data, won’t do any replacing or deletion…

    Sami

  5. Doh. I wonder if you can write a backspace/delete char? 🙂

  6. Ray,

    Yikes, that would really difficult, I though CF should make things easy. 😛

    Seriously though, thinking about doing that raises the hair on the back of my neck, if its possible at all.

    I’ll keep digging, must be a JAVA solution out there.

    Sami

  7. Bob Courchaine said:

    Why is the line number important?

    The line numbering I’m thinking of comes from an external editor or something like that. In other words, an unnecessary label.

    I’d try to get rid of the line number concept, using something like rereplace().

  8. Bob,

    I’m trying to build something generic, where I know, for example, that I’d like to change text on line X, delete line Y. I might not know the patterns to employ Regex. Again, this is for a specific use case. If we can come up with something generic, I can see a lot of people using something like this.

  9. Bob Courchaine said:

    hmm-

    Another label other than line _number_ that’s already a part the text file after it’s read would be line *end*.

    Count the CRLFs or LFs in cffile’s variable to get your line number.

  10. Bob,

    Yes, that is similar to the array solution, and expensive. Sigh.

  11. Bob Courchaine said:

    At first blush can’t see how it could be done any more economically, Sami.

    You’re only loading it into memory once.

    You’re right- using the array approach you’re reading it into one var then creating the array in another.

    Then again, how much memory are we really talking about? Are we optimizing before we have to?

    Obviously my curiosity is piqued! Pls do update us if you find another, cheaper way.

  12. Jason Delmore said:

    I believe the most efficient way, at least from a memory utilization standpoint, is to create a bufferedreader to read the file line by line, and a bufferedwriter to write the file line by line. When you get to the line you want to change, you just write it directly.

    It may be faster to use a stringbuilder and write the file once, but only if the file is not so big that memory is an issue. Either way, the first method should be pretty quick.

    🙂
    Jason

  13. Sami, why not use the native file operations? According to livedocs, fileRead() does not read the entire file into memory, I would assume that fileReadLine() works the same way.

    Here is a snippet of code I put together that uses the file operations to “change X on line Y”

    http://pastebin.com/m5d63d68d

  14. Jalpino,

    I’ll definitely evaluate your code!

  15. I’m trying to do something similar. ( http://groups.google.com/group/dfwcfug/browse_thread/thread/1f2aca859c0f92a7 ) I also couldn’t find a way to remove a line from the text file. I might a different approach.

  16. Where can i have more info on this ?

    Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: