Reputation: 735
I have a JSON array that I want to write it into a file. Everything works perfectly about writing the file but the problem is when I try to read it gives me a lot of uncoded text as ……. The text is in Arabic language.
This is my code to read the file:
FileInputStream fis = openFileInput("File");
BufferedInputStream bis = new BufferedInputStream(fis);
StringBuffer sb = new StringBuffer();
while(bis.available() != 0){
char c = (char) bis.read();
sb.append(c);
}
JSONArray read = new JSONArray(sb.toString());
for(int x = 0; x < read.length(); x++){
JSONObject readOb = read.getJSONObject(x);
String id = readOb.getString("id");
String name = readOb.getString("name");
Toast.makeText(Main.this, "Id: " + id + "Name: " + name , Toast.LENGTH_LONG).show();
}
bis.close();
fis.close();
It would be great if anybody would suggest any solution to make text appear perfectly.
Edit:
This is what I tried to append it to StringBuffer.
fis = openFileInput("Test");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
StringBuffer sb = new StringBuffer();
while(isr.read() != 0){
sb.append(isr.read());
}
Upvotes: 0
Views: 1357
Reputation: 39406
this
char c = (char) bis.read();
is wrong. JSON
is (most of the time) encoded in UTF-8
, meaning a byte is not a char. You need to use a Reader
, which decodes a stream using an encoding.
For example:
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
Then you can read, it will return a complete UTF-8
character, which can be 1 to 4 bytes.
Here is how to completely and efficiently read the file:
FileInputStream fis = openFileInput("My Books");
Reader reader = new InputStreamReader(fis, "UTF-8");
char[] buf = new char[1024];
int red = -1;
StringBuffer sb = new StringBuffer();
while ((red = reader.read(buf)) != -1) {
sb.append(buf, 0, red);
}
fis.close();
(which is quite close to the Apache EntityUtils implementation of reading an InputStream to a String)
I strongly discourage using BufferedReader.readLine
, as seen in many places, for 2 reasons:
BufferedReader
is interesting when reading from the network, and readLine
when reading bounded-size lines, because it limits the number of read calls when the link is slow, and allow for line-by-line treatment.
Upvotes: 1
Reputation: 3212
While reading the data from the file try to encode it
Charset.forName("UTF-8").encode(sb.toString())
Upvotes: 0