B4J Question Search any string inside file + replace string with another

amorosik

Expert
Licensed User
L'obiettivo e' realizzare la classica procedura 'search-and-replace' ed in futuro magari anche un editor esadecimale
Che possa operare correttamente su file di grandi dimensioni (diversi gygabyte) e contenenti caratteri di qualsiasi tipo
I problemi contro i quali mi sto scontrando sono:
- come aprire il file in modalita' binary, per leggere byte per byte o gruppi di byte?
- data una stringa qualsiasi (sia alfanumerica che contenente caratteri di controllo) come trovarla all'interno del file, si va di byte in byte o c'e' una modalita' piu' efficace?
- una volta trovata la posizione iniziale della stringa cercata, come eseguirne la sostituzione con la stringa2 (che potrebbe avere anche lunghezza diversa)?


I'm sorry
The goal is to create the classic 'search-and-replace' procedure and in the future maybe even an hex editor
That it can operate correctly on large files (several gygabytes) and containing characters of any type
The problems I'm struggling with are:
- how to open the file in binary mode, to read byte by byte or groups of bytes?
- given any string (both alphanumeric and containing control characters) how to find it in the file, do you go from byte to byte or is there a more effective way?
- once the initial position of the searched string has been found, how to replace it with string2 (which could also have a different length)?
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Please write in English otherwise we do not understand what you are asking.

However, you can use file.readByteS and the jRandomAccessFile library or B4XCollections (B4XByteBuilder class) to manage single and group bytes
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
You cannot directly replace unless the new value is the same length as the old value.
I agree that doing this is not as straightforward if the old and new values are not the same length, but...

if hard-pushed for disk space and if "directly" means "in-place in the one file" then it is possible if the new value is longer than the old value, albeit takes two passes: first to count the occurrences, second to do the replacement, starting from the end of the file and working towards the beginning.

Can also be done in-place if new value is shorter than old value, if you're ok with trailing nulls or spaces padding the end of the file (or if the OS has an accessible way of reducing the length of a file, but I wouldn't hold my breath on this).
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at my HexViewer, it doesn't allow editing, but reads files of any size and provides a search facility. It may give you a start. Full source code is in the post.

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…