r/javahelp • • May 23 '20

Recommendations on String manipulation

A company I work with is replacing an old COBOL process with Java.

Pretty much the business rules are about reading some long lines from a text file, do some snippin and reordering before writing to a new file text.

That part is covered, but I'm doing some heavy use of String.substring. I haven't seen a real input file, but they are supposed to hold near 2 million lines each.

Because of that, I would like to make the new process as memory efficient as possible. (When I first learned Java they always warned us about the perils of String)

The process will run on a server (don't know the exact specs) if that's useful.

Thanks in advance.

17 Upvotes

11 comments sorted by

View all comments

8

u/Philboyd_Studge May 23 '20

Creative use of regex might do the trick, but for pure speed and memory usage, using char arrays would probably be the best.

2

u/frostbyte650 May 23 '20

Does java not treat strings as char arrays?

7

u/joefooo May 23 '20

Char arrays are mutable, Strings are not

2

u/Philboyd_Studge May 23 '20

It does internally. And it may not be worth it to make your own routines, but if you have some fairly well-behaved data you might be able to optimize.

1

u/Yithar Intermediate Brewer May 23 '20

Internally String uses them but as said strings are immutable. That means more memory usage.

1

u/[deleted] May 23 '20

Fortunately the lines (and each field) have a fix length, so I don't need to search for them.

I'll give it a try to char array, I think I can make that change easily.

Thanks