11

There is a 100,000-character text that need to be displayed. If I put it into String object, I get an error "constant string too long". The same is with StringBuffer object.

StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Long text here........"); //<-- error

Is there a solution to this beside cutting the text into smaller texts?

sandalone
  • 41,141
  • 63
  • 222
  • 338

4 Answers4

10

I think the length of constant strings in java is limited to 64K -- however, you could construct a string at run time that is bigger than 64K.

Sai
  • 3,819
  • 1
  • 25
  • 28
  • 1
    Could you give me a short code reference to what you were saying?! Thanks – sandalone Jun 11 '11 at 21:40
  • You could do something like Tomalak is suggesting -- basically composing the string at runtime. – Sai Jun 11 '11 at 21:52
  • What do you mean under "construct a string at run time that is bigger that 64K" ? – sandalone Jun 12 '11 at 06:20
  • 3
    You cannot initialize it with a string literal that is larger than 64K. However, for instance, you can have this string in a file that you read in at run time and assign it to a string. – Sai Jun 12 '11 at 12:42
  • What should I do with formatting? It's a text file and I need to format some lines (for example, increase the font, bold, etc.) ??? I cannot format it inside txt file. – sandalone Jun 12 '11 at 12:56
  • Not sure what you are trying to do; You may possibly have to programmatically handle this. – Sai Jun 12 '11 at 13:09
  • For example, I want title of paragraphs be bold and with font increased. How can I do it programmatically? It's not the same like I'm working with XML. – sandalone Jun 12 '11 at 13:24
  • 1
    If you are talking HTML, is it possible to generate it before hand and load it to a file that you read in later? Or generate text with markers indicating paragraph breaks and then process the string after reading it in. I think I might be misunderstanding what exactly you are trying to do. But basically it appears that you either have to preformat it in the file itself or have structural markers that you can interpret at runtime to generate the required HTML tags – Sai Jun 12 '11 at 14:04
  • It's a simple .txt file, but I think I will try to make some king of unique markers and format such lines accordingly. Thanks – sandalone Jun 12 '11 at 14:06
6

Strings Longer than 64k are not allowed to be used directly in java, but you can use them indirectly.

  • Step 1) click on your string
  • Step 2) press Alt + Enter keys together
  • Step 3) Select Extract String resource
  • Step 4) Name the resource and hit enter key.

That is it. It will generate a string for you in strings.xml If you already have string in your strings.xml you can use this code to retrieve it:

String yourStr = getString(R.string.sampleBigString);
2

Yes constant String has a limit in java.

So what you can do is copy your String in a text file and put in root of Assets folder and read from file by the following method.

public String ReadFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
    fIn = context.getResources().getAssets()
            .open(fileName, Context.MODE_WORLD_READABLE);
    isr = new InputStreamReader(fIn);
    input = new BufferedReader(isr);
    String line = "";
    while ((line = input.readLine()) != null) {
        returnString.append(line);
    }
} catch (Exception e) {
    e.getMessage();
} finally {
    try {
        if (isr != null)
            isr.close();
        if (fIn != null)
            fIn.close();
        if (input != null)
            input.close();
    } catch (Exception e2) {
        e2.getMessage();
    }
}
return returnString.toString();
}
Rahul Jain
  • 276
  • 2
  • 11
1

I had same problem and the solution was the very big string literal that was assigned to one constant, to split it to several smaller literals assigned to new constants and concatenate them.

Example:

Very big string that can not compile:

private static String myTooBigText = "...";

Split it to several constants and concatenate them that compiles:

private static String mySmallText_1 = "...";
private static String mySmallText_2 = "...";
...
private static String mySmallText_n = "...";

private static String myTooBigText = mySmallText_1 + mySmallText_2 + ... + mySmallText_n;
jascadev
  • 19
  • 1
  • 1
  • 5