InputStreamReader、FileInputStream 从中点开始解析文件而不是开始
我正在尝试解析 UTF 16 编码的 JSON 文件,但是遇到了一个奇怪的问题。
每当我使用 FileInputStream 时,解析文件似乎都是从中点开始的。例如,如果文件长度为 40 个字符,则它将从第 20 个字符开始。这会导致解析 JSON 时出错,因为显然其数据从文件中的第 0 个字符开始。
尽管工作了几周,但这个问题前几天突然出现。我看不出我的代码有任何问题,因为在问题开始之前的几天里它没有改变。
我尝试的解决方法之一是改用 FileReader
。它通常从字符零开始,但是它无法处理文档中的 UTF-16 字符,因此不能解决问题。
我正在使用 Google 的 Gson 库来处理 JSON,但我认为问题出在 InputStreamReader
或 FileInputStream
中的某个位置。
以下是有问题的代码;
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(file), "UTF-16"));
reader.beginArray();
...
这是它抛出的错误。上面的 reader.beginArray();
行导致了异常。
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 21
at com.google.gson.stream.JsonReader.expect(JsonReader.java:337)
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:304)
at reader.ProofDatabase.load(ProofDatabase.java:130)
...
这是我的部分解决方法,它不处理 UTF16 字符串
JsonReader reader = new JsonReader(new FileReader(file));
reader.beginArray();
...
任何解决方案,无论是对原始问题的修复,还是以 UTF-16 形式读取文件的另一种方法,都非常受欢迎。
I'm attempting to parse a UTF 16 encoded JSON file, however I've run into a weird issue.
Whenever I use a FileInputStream
, parsing the file seems to start at the midpoint. For example, if the file is 40 characters long, it will begin at character 20. This causes errors with parsing the JSON, as obviously its data begins at character 0 in the file.
This issue cropped up the other day, despite working for weeks. I can see no issue with my code as it wasn't changed in the days leading up to the issue starting.
One of my attempted workarounds was to switch to using a FileReader
. It begins normally at character zero, however it cannot handle the UTF-16 characters in the document, so does not solve the problem.
I am using Google's Gson library for handling JSON, however I think the issue lies somewhere within the InputStreamReader
, or with FileInputStream
.
Below is the code which is at issue;
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(file), "UTF-16"));
reader.beginArray();
...
Here is the error it throws. The line reader.beginArray();
above causes the exception.
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 21
at com.google.gson.stream.JsonReader.expect(JsonReader.java:337)
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:304)
at reader.ProofDatabase.load(ProofDatabase.java:130)
...
And here is my partial workaround which does not handle UTF16 strings
JsonReader reader = new JsonReader(new FileReader(file));
reader.beginArray();
...
Any solution, be it a fix to the original problem, or another method of reading in the file as UTF-16 would be more than welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我找到解决方案时忘记更新问题。
该错误是由于我手动创建 JSON 文件而不是以编程方式生成它。
当 JSONWriter 类生成文件时,额外的元数据会添加到文件中,告诉解析器它是一个 JSON 文件。手动创建的文件中缺少此元数据,因此 JSONReader 在解析文件时抛出错误。
Forgot to update the question when I found the solution.
The error came from the fact that I manually created the JSON file rather than programmatically generating it.
When the file was generated by the JSONWriter class, extra meta-data was added to the file that told the parser that it was a JSON file. This meta-data was missing in the manually created file, so JSONReader was throwing errors in parsing the file.