简体   繁体   English

Android从文件中读取大字符串

[英]Android read in large String from file

In my application, when it loads up, it makes a web call which retrieves a JSON string. 在我的应用程序中,加载后会进行网络调用,以检索JSON字符串。 I want to store this JSON string to the phone so the application won't crash if the user loses connection. 我想将此JSON字符串存储到电话中,因此如果用户断开连接,应用程序将不会崩溃。 JSON string is required to set the text of the application. 设置应用程序的文本需要JSON字符串。 So I am doing this 所以我正在这样做

    File myDir = new File(getFilesDir().getAbsolutePath());

                FileWriter fw = new FileWriter(myDir + "/jsonMain.txt");
                fw.write(sJsonMain);
                fw.close();

And then to retrieve it I am doing this 然后要检索它,我正在这样做

    BufferedReader br = new BufferedReader(new FileReader(
                            myDir + "/jsonMain.txt"));
                    sStoredJSON = br.readLine();

It works fine with other JSON strings, however it is not with the Large JSON string. 它可以与其他JSON字符串配合使用,但是不适用于Large JSON字符串。 I checked the log and it says this 我检查了日志,上面写着

04-26 10:00:44.511: I/global(893): Default buffer size used in BufferedReader constructor. 04-26 10:00:44.511:I / global(893):BufferedReader构造函数中使用的默认缓冲区大小 It would be better to be explicit if an 8k-char buffer is required. 如果需要8k字符缓冲区,则最好是明确的。

Could someone please tell me how else I can read in a large string like this? 有人可以告诉我,我还能怎样读一个大字符串吗?

Thanks in advance!! 提前致谢!!

try this: 尝试这个:

BufferedReader br = new BufferedReader(new FileReader(
                        myDir + "/jsonMain.txt"), 8); // or a higher value than 8
sStoredJSON = br.readLine();

您可以在BufferedReader 构造函数上添加缓冲区大小,因此可以使用先前的File对象来确定文件长度并相应地创建缓冲区大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM