• Post Reply Bookmark Topic Watch Topic
  • New Topic

change ID number in multiple files  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

Say I have a text file:

I love bananas.
<age> 23 </age> years old.
I like beaches.


All I want to do is change the age to 32 or 25. The change would also be a 2 digit number in the same position. Any way to do that in Java WITHOUT reading and writing the whole file? I know its possible with buffered reader and writer, but I am looking for a less memory intensive way when the file gets to be huge and I have to do it for 100 files in a directory.

Also can I use this? Not sure if it helps but I am looking into it now. As I understand I can replace those two numbers as bytes?

Thanks!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to do it without reading the whole file at all?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something definitely got lost in context...

Shaman Dasuta wrote:
Also can I use this? Not sure if it helps but I am looking into it now. As I understand I can replace those two numbers as bytes?

Thanks!


What is this "this" and "it" you are referring to?

Henry


PS... have you considered the RandomAccessFile class?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use a random access file without going through the whole file? Obviously random access will work, but OP said specifically to avoid going through the whole file.

By the way, you cannot replace two numbers by bytes. What you have there as 23 is not a number, but two characters. If you use an encoding like UTF8 or ISO8859‑1 (or even ASCII), then those two characters will occupy 8 bits each in memory and you can change two bytes to replace those two characters.
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong
I actually forgot to post RandomFileAccess demo code.

@Campbell How to get started with Random FileAccess?

So far I have this:

String line = null;
RandomAccessFile file = new RandomAccessFile("C:/java_1.txt", "rw");
System.out.println(file.getFilePointer());
while((line = file.readLine()) != null){
System.out.println(line);
System.out.println(file.getFilePointer());

if(line.contains("23")){
file.seek(file.getFilePointer());
file.write("24".getBytes());
break;
}
}
file.close();

This should work? I am going to try it with nonsensitive files first.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaman Dasuta wrote: I am going to try it with nonsensitive files first.

you should ALWAYS do it with test files first.

Hell..I'd even say when you do it for real, you should make a copy of the source file, change the copy, validate it worked right, and ONLY if it did, delete the original.
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd even extend fred's comment. Original files keep as long as you can, even if you think you won't need them anymore - it might will change.

Don't you have a problem with this in windows? "C:/java_1.txt", shouldn't be a "C://java_1.txt"?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah..disk space is cheap. having some kind of long-term storage and archival system is critical in any business. My team has terrabytes of data archived. We keep a rolling three months, with files archived daily.

And we are not in the business of storing data - we just pass data through.

There is no reason NOT to keep the original file for a long, long, long time.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java® Tutorials? That part has a section about random access files.
 
Liutauras Vilda
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Don't you have a problem with this in windows? "C:/java_1.txt", shouldn't be a "C://java_1.txt"?
Overlooked, OP's way is correct, in windows "\", so in that case would be "\\".

Agree with fred. We keep actual "data" files at least for past 5 years. And since the files are being processed, from time to time still appear situation when original files are needed. So, you never know.
Shaman Dasuta, as guys you already suggested, never work on the file if you don't have a copy of it. Being a very scrupulous: original file is not exactly the same as copy of original file, in some specific situations it can become important - so better is to work on copies, rather than original files.

[edit] Probably I was too much out of context lines
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

Got it. I always break down a complex problem to something super easy like change an age on a text file and go from there.

In the end I found this to work out when you want to replace an Old Number with a New Number:



Important to Note:
There are two others methods: Filename Utility in Apache and Random File Access Method.
The File Name utility needs a JAR File. Easy to Use.

Random File Access I didn't get to try out.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And every age is 23?
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if it finds "1234" ? Or, is this not possible?
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And every age is 23?


So I just increment the new number on a for loop. It works out since I do a Integer to String for the string.replace command.

Otherwise you can do whatever you want to NewNumber for the iteration you are working on.

Carey BROWN wrote:What happens if it finds "1234" ? Or, is this not possible?


So that was just an example. Technically if 23 is anywhere else in the document it will overwrite that and make it 25 or what ever you set it to.

In the actual program I need to replace a 6-10 digit number in multiple files. Luckily very few other numbers are that big and would have such a similarity/clash.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shaman Dasuta wrote: . . .
In the actual program I need to replace a 6-10 digit number in multiple files. Luckily very few other numbers are that big and would have such a similarity/clash.
That means you will have very few errors in the new file and the fewer the errors the more difficult they are to find and the harder they are to correct.

No, I don't think that method will work. Not unless you have some reliable mechanism for identifying the number to change.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!