I'm trying to get a JSON string from a file saved under the downloads folder of an Android device. Now I'm able to get a path to this just fine using a filebrowser provided here.
I attempted to use the idion given here to open the file and read out a string. (evidently Android doesn't use Java 7? I am unable to use the answer from this question, the only Files class I find is under android.provider.MediaStore)
In any case, I get (sort of) the JSON string using the idiom from the question. Problem is I can't use the output with GSON (error: Expected BEGIN_OBJECT but was STRING at line 1 column 1)
I suspect it's because what I'm getting back from the file to string function looks like this:
����z������9>{
"chEqEnable": [
[
true,
true,
... etc ...
Also occasionaly values for say, a boolean looks like:
z������ true,
instead of
true,
Is there some better way when dealing with Android and external storage, to take my file and read a JSON string from it, abberation free?
For reference, here is what my implementation of the idiom from the other question looks like:
public static String file2String(String filePath) throws IOException
{
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line = null;
try{
while( (line = reader.readLine()) != null)
{
stringBuilder.append(line);
}
} finally {
reader.close();
}
String returnStr = stringBuilder.toString();
return returnStr;
}
When I write a file to external storage I use this
public static String ExportTuning(Context context, String fileName, String json)
{
File path;
if(fileName.contains(".rfts")) {
path = GetDocumentsPath(fileName);
} else {
path = GetDocumentsPath(fileName+".rfts");
}
try {
FileOutputStream fileOut = new FileOutputStream(path);
ObjectOutput strOut = new ObjectOutputStream(fileOut);
strOut.writeUTF(json);
// strOut.writeChars(json); // this just causes a ton of gibberish when I read the file back
strOut.close();
fileOut.close();
String[] paths = new String[]{path.getAbsolutePath()};
MediaScannerConnection.scanFile(context, paths, null, null);
return "Save Successful";
}
catch(java.io.IOException e) {
return e.getMessage();
}
}