如何在Android的StringBuffer中连接long String

时间:2015-04-15 14:07:07

标签: android arrays arraylist stringbuilder stringbuffer

我在StringBuffer concatination中面临一个问题,即从JSONArray追加String的大字符。 我的数据非常庞大,并且在正确迭代了205个数组的索引之后进入日志 但是当我在JSONArray中追加StringBuffer或StringBuilder中的每一行String时,它只占用4063个字符而不附加JSON数组中存在的所有字符,但迭代不会中断并一直持续到204行。

                String outputFinal = null;

                try {

                    StringBuilder cryptedString = new StringBuilder(1000000);

                    JSONObject object = new JSONObject(response);

                    JSONArray serverCustArr = object.getJSONArray("ServerData");

                    Log.d("TAG", "SurverCust Arr "+serverCustArr.length());

                    for (int i = 0; i < serverCustArr.length(); i++) {

                        String loclCryptStr = serverCustArr.getString(i);

                        Log.d("TAG", "Loop Count : "+i);

                        cryptedString.append(loclCryptStr);
                    }

                    Log.d("TAG", "Output :"+cryptedString.toString());


                    CryptLib _crypt = new CryptLib();

                    String key = this.preference.getEncryptionKey();

                    String iv = this.preference.getEncryptionIV();

                    outputFinal = _crypt.decrypt(cryptedString.toString(), key,iv); //decrypt
                    System.out.println("decrypted text=" + outputFinal);

              } catch (Exception e) {

                e.printStackTrace();
              } 

我的JSONArray在205中联系了119797个字符,并且在迭代之后附加到StringBuffer中,我必须用带有字符串进行解密的库对其进行解密。但是StringBuffer没有119797个字符的完整数据。

而Exception是因为字符串不完整,我将文件放在下面的链接上以供参考,并使用跨平台CryptLib使用AES 256进行加密在Github上轻松找到

3 Files With Original and Logged text

1 个答案:

答案 0 :(得分:1)

不要使用StringBuffer,而是使用StringBuilder ..这里有详细的解释

http://stackoverflow.com/questions/11908665/max-size-for-string-buffer

希望这会有所帮助。 :)

修改

这是我用来读取整个字符串的代码......

public void parseLongString(String sourceFile, String path) {
        String sourceString = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(sourceFile));
            // use this for getting Keys Listing as Input
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
            br.close();
            sourceString = sb.toString();
            sourceString = sourceString.toUpperCase();

            System.out.println(sourceString.length());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        File file = new File(path);
        FileWriter fileWriter = null;
        BufferedWriter bufferFileWriter = null;
        try {
            fileWriter = new FileWriter(file, true);
            bufferFileWriter = new BufferedWriter(fileWriter);
        } catch (IOException e1) {
            System.out.println(" IOException");
            e1.printStackTrace();
        }
        try {
            fileWriter.append(sourceString);
            bufferFileWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }

    }

这是outPut文件,我只是将其转换为大写。

https://www.dropbox.com/s/yecq0wfeao672hu/RealTextCypher%20copy_replaced.txt?dl=0

希望这有帮助!

编辑2

如果你还在找东西..你也可以试试STRINGWRITER 语法将是

StringWriter writer = new StringWriter();
        try {
            IOUtils.copy(request.getInputStream(), writer, "UTF-8");
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        String theString = writer.toString();