String error "string too long"

There is 100,000 character text that needs to be displayed. If I put it in a String object, I get the error message "constant string too long". Same thing with a StringBuffer object.

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

Is there a solution for this, in addition to reducing the text to smaller texts?

+7
source share
3 answers

I think that the length of constant lines in java is limited to 64K - however, you can build a line at runtime that exceeds 64K.

+7
source

Strings Longer than 64k are forbidden to use directly in java, but you can use them indirectly.

  • Step 1) click on the line
  • Step 2) press Alt + Enter together
  • Step 3) Select "Extract String Resource"
  • Step 4) Name the resource and press enter.

That's all. It will generate a string for you in strings.xml If you already have a string in strings.xml , you can use this code to get it:

 String yourStr = getString(R.string.sampleBigString); 
+1
source

Yes String constant has a limit in java.

So what you can do is copy the String string into a text file and paste it into the Assets root folder and read from the file in the following way.

 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(); } 
0
source

All Articles