Objeto de cadena única para una cadena grande en Java

Is it a good practice to store a large string (over 300000 chars) in a sole String object in case of memory management and performance or there's better way?

preguntado el 10 de marzo de 12 a las 14:03

2 Respuestas

Johan Sjöberg's explanation is good:

"It's how to use the String which would determine the practice."

If 300,000 characters that make up your String object don't reside in a Java source file enclosed in double quotes(" "), e.g. residing in an external file, then you can use different techniques to get the String content chunk by chunk or as a whole. Take a look at this example:

FileInputStream fis = new FileInputStream( "yourFile.txt" );
BufferedInputStream bis = new BufferedInputStream( fis );
byte [] buffer = new byte [ 8 ];
int bytesRead = 0;
String chunk = "";

while ( ( bytesRead = bis.read( buffer ) ) != -1 )
    chunk = new String( buffer, 0, bytesRead );

Assuming you handle possible exceptions, in the code above you get a string of size 8 bytes every time you enter the while loop. So by modifying the byte array size which is 8 in this code, you can change the chunk size and use chunks anywhere else, print them etc.

In your example, you say your string has around 300000 characters. So setting the size of the byte array buffer a X 4 300,000 1,200,000 = will enable you get a single string chunk that contains all the characters.

respondido 10 mar '12, 15:03

Java represents Strings using UTF-16 encoding, hence in worst case each character is 4 bytes. That gives you roughly a 4*300000/1024/1024 ~ 1.14MB String, which isn't that bad in itself.

It's how you utilizado the String that determines the practice.

respondido 10 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.