简体   繁体   English

如何搜索文件内容并只读取文件的一部分

[英]How to search for contents of file and read only a part of File

I am using JSON-RPC.我正在使用 JSON-RPC。 When i send request to server i get a response from server which is almost 2MB long.当我向服务器发送请求时,我从服务器收到了将近 2MB 长的响应。 There is no new line character in the data.数据中没有换行符。 As we know, it is not possible to store all the 2MB data inside string, i had to write the data to a file.正如我们所知,不可能将所有 2MB 数据存储在字符串中,我必须将数据写入文件。

This is what my file looks like:这是我的文件的样子:

[{"data1":"data1",....},{...},...] [{"data1":"data1",....},{...},...]

I want to read the data part-by-part, like get the string between "{" and "}", so that i get the exact contents of 1 JSON Object, after which i can parse the data.我想逐部分读取数据,比如获取“{”和“}”之间的字符串,以便获得 1 JSON Object 的确切内容,之后我可以解析数据。

I have referred to this link , but I won't get the position of "{" and "}" to copy my JSON data.我已经参考了这个链接,但是我不会得到“{”和“}”的position来复制我的JSON数据。

Is there a way to implement such a thing?有没有办法实现这样的事情?

You can also use您也可以使用

com.google.gson.stream.JsonReader.JsonReader com.google.gson.stream.JsonReader.JsonReader

And compile your object as you get the data from the server.并在从服务器获取数据时编译 object。 No need for storing to a String or writing and reading from a file无需存储到字符串或从文件中写入和读取

You should use a regex pattern您应该使用正则表达式模式

More or less something like this:或多或少是这样的:

Pattern pattern = Pattern.compile( "{(.*?)}" );
Matcher matcher = pattern.matcher( response );
while( matcher.find() ) {
  String jsonString = "{" + matcher.group( 0 ) +"}" ;
  //parse the json
}

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

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