Bookmark Topic Watch Topic
  • New Topic

replacing parts in Files  RSS feed

 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi everyone!
I would like to do the following:
I have a File and some strings i want to find in it. Since the strings can repeat themselves, i need to memorize my last position(RandomAccessFile?)and search for the next string from there. Is there an easier way to do this than reading from line to line? Reading lines is trouble, since a string i'm looking for, might contain more than one line.
[ December 09, 2005: Message edited by: Csaba Kassitzky ]
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Using RandomAccessFile to change a file's content is problematic because unless the data you are replacing is exactly the same size as the replacement, you will either be overwriting existing data or leaving some of the old data. Have a look at the Java IO Tutorial. I think a Reader may be more what you are looking for. You can read data in, replace the data as you go and write the results to a temp file. Once you are finished, replace the original file with the temp file.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
No, as a matter of fact, if i change anything, i only want to insert some stuff. Anyways, i think reading characters in might be the solution to my problem if one thing applies: If i read these characters in a string, and after that i insert something in the beginning of the string, will the linebreaks stay where they were when i write the string to the output?
I can't write char to char, because i need to load big stuff until i can confirm if it was my string to insert before. Here's the code i found:

Will this work like i would only write c every time?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Csaba Kassitzky:
If i read these characters in a string, and after that i insert something in the beginning of the string, will the linebreaks stay where they were when i write the string to the output?

If you don't move them, the line breaks stay where they are.

Originally posted by Csaba Kassitzky:

Will this work like i would only write c every time?

You can read and write characters but you need to be able to compare some buffer of characters to the string you are looking for. Depending on what you are doing with the replacement data, you may not want to write data until you are sure it doesn't match the comparison string.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Report post to moderator
You're on the right track, I think, but I don't know what size chunks you have to read to find the insert point. For text files it's common to see people read one line at a time. Put a buffered reader on your file and readln() until null.

If you need chunks other than a single line to detect the insertion point, save them up just as they come in so you can write them back out whether you insert the new stuff or not. It may be a challenge deciding just how to store them so you can inspect them for the insertion point and write them out. If that's the problem, tell us more about the data structure and the indicator you are looking for.

Routines to change or delete lines are very similar ... write a different line or write nothing at all and throw away the input line.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ok, here's what i want to do:
I need to compare sentences in a HTML file.
For that i pick the sentences out from the file.
I use my algorithm to do the comparisons.
After that i reopen the file and search for the
sentences, that were modified(using the same picking alg.)
When i found a sentence, i check if it was modified,
if yes, i put a <font color=blue> before it, and
a </font> after it. So you're saying if i read chars in and concat
them in the string like:

the linebreaks will stay?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Csaba Kassitzky:
the linebreaks will stay?


Line breaks are simply characters. Unless you do something with them, you will simply read them in and write them out. Note that if you use BufferedReader.readLine() as Stan suggests above, the String returned will not have the line break in it.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
That's exactly why i want to avoid readline.
Thanks for the help!
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
If i use this:

How can i get the character back(to check where my sentence is)?
What i need to do:
Read the characters and concat them into a string. When i encounter a ".", which means the sentence is complete, i check if that sentence is what i'm looking for. If yes, then i put the 2 HTML tags around this sentence and write it back. Can anyone tell me how to do this? Linebreaks are needed!
Please someone help me, i'm getting more and more confused!
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think the only problem i got now is, how to convert the int into
a char. Btw, what is the int value of linebreak?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Report post to moderator
I'm trying to think of ways to capture a sentence to work with.

With HTML it will be fairly challenging to skip all the tags and only inspect the sentences. If you stay with your character processing, you'll need something like:

Does that make sense? I didn't try to handle tags in the middle of sentences, like a bold word or a link. I'm not sure where newlines come into play.

Can you look into some completely different approaches?

One would be to read the whole file into one string and use regular expressions to match and inspect one sentence at a time. This is tricky but there are some good RegEx experts here at the ranch.

Another would be to use a good HTML parser to help you find the text between the tags. I like the Quiotix Parser because their "visitor" design makes it easy to process text and recreate the HTML with any changes you make.
[ December 10, 2005: Message edited by: Stan James ]
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
This discussion is continued here
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!