aspose file tools*
The moose likes Java in General and the fly likes find and replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "find and replace" Watch "find and replace" New topic
Author

find and replace

sinchen kutty
Greenhorn

Joined: Sep 13, 2004
Posts: 3
Hi,

I have two files.first file:

alphabets name

A - first letter
B - second letter
C - third letter
.
..
.
....

like that and the second file is:

Person Name Position

AndyAMnagaer
BeateBClerk
CindyCSecretary
...........
.......
.........


Now my doubt is that when i read the second file using streams and if i find the name as A,B or C then i have to replace them with their names in the first file like A-first letter;B-second letter;C-third letter.........I am bit confused about the structure of the program or how to read first file and make changes in the second file.
My program for reading the second file is:


[1] Note: if the values[1]==A then it should be changed to "firstletter" or if its B then it should be chnaged to "Second letter" by reading the first file.How to insert here the coding and the structure please.

[ edited to preserve formatting using the [code] and [/code] UBB tags, to remove the evil tab characters, and to move a comment and question out of the code -ds ]
[ September 17, 2004: Message edited by: Dirk Schreckmann ]
rajakumar babu
Greenhorn

Joined: Nov 12, 2003
Posts: 9
Find and Replace is pretty simple, all you need to do is, for every line you read from 1st file, you want 'xxx' to be replaced with some text, so call a method by passing line as parameter and in that method, you read second file and return a match string to replace. So, the calling method would have code something like this:

While((line = someReader.readLine()) != null) {
String replaceString = readSecondFileAndReturnString(String line);
...........
...........
}

Hope this helps you
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
A good solution might be to read the entire contents of the first file into a map data structure. As you come across keys in the second file, use them to look up the replacement values in the map, populated from the first file.

Concerning replacing part of a string, are you able to replace the "XXX" in the string, "How are you, XXX?"

An easy way to effectively change the contents of a file might be to simply create a differently-named new file with the desired contents, completely delete the old file, then rename the new file to that of the deleted old file. Or you could read the entire contents of the old file into memory, apply the content changes in memory, delete the old file (or just the contents of the old file), then create a new one with the same name and write the changed data to it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find and replace