String error “constant string too long”

前端 未结 4 1037
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 06:29

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.

4条回答
  •  悲哀的现实
    2021-02-08 06:44

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

提交回复
热议问题