打开 Android 外部存储中的文件并读取 JSON 字符串
Open File in Android External Storage and read JSON String
我正在尝试从 Android 设备的下载文件夹下保存的文件中获取 JSON 字符串。现在,我可以使用提供的文件浏览器 here.
获得通往此路径的路径
我试图使用给定的 idion here 打开文件并读出一个字符串。 (显然 Android 不使用 Java 7?我无法使用这个问题的答案,我找到的唯一文件 class 在 android.provider.MediaStore 下)
无论如何,我使用问题中的成语得到(某种程度上)JSON 字符串。问题是我不能使用 GSON 的输出(错误:预期 BEGIN_OBJECT 但在第 1 行第 1 列是 STRING)
我怀疑这是因为我从文件返回到字符串函数看起来像这样:
����z������9>{
"chEqEnable": [
[
true,
true,
... etc ...
也偶尔会有值,例如布尔值:
z������ true,
而不是
true,
在处理 Android 和外部存储时有没有更好的方法来获取我的文件并从中读取 JSON 字符串,无差错?
作为参考,下面是我对另一个问题中习语的实现:
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;
}
当我将文件写入外部存储时,我使用这个
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();
}
}
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?
你的主要问题出在ExportTuning()
方法上。你没有写出 JSON。您正在将 Java 对象写出到 ObjectOutputStream
。如果你想写 JSON,摆脱 ObjectOutputStream
并使用写文本的东西,而不是对象(例如,PrintWriter
)。请注意,您可以使用您最喜欢的文本编辑器或 cat
或其他任何工具检查 JSON 文件,以查看它是否包含您期望的内容。
在稍后解析 JSON 方面,删除所有读入字符串的代码,创建一个 Reader
对象(例如,一个 InputStreamReader
),以及 pass that to fromJson()
.
我正在尝试从 Android 设备的下载文件夹下保存的文件中获取 JSON 字符串。现在,我可以使用提供的文件浏览器 here.
获得通往此路径的路径我试图使用给定的 idion here 打开文件并读出一个字符串。 (显然 Android 不使用 Java 7?我无法使用这个问题的答案,我找到的唯一文件 class 在 android.provider.MediaStore 下)
无论如何,我使用问题中的成语得到(某种程度上)JSON 字符串。问题是我不能使用 GSON 的输出(错误:预期 BEGIN_OBJECT 但在第 1 行第 1 列是 STRING)
我怀疑这是因为我从文件返回到字符串函数看起来像这样:
����z������9>{
"chEqEnable": [
[
true,
true,
... etc ...
也偶尔会有值,例如布尔值:
z������ true,
而不是
true,
在处理 Android 和外部存储时有没有更好的方法来获取我的文件并从中读取 JSON 字符串,无差错?
作为参考,下面是我对另一个问题中习语的实现:
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;
}
当我将文件写入外部存储时,我使用这个
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();
}
}
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?
你的主要问题出在ExportTuning()
方法上。你没有写出 JSON。您正在将 Java 对象写出到 ObjectOutputStream
。如果你想写 JSON,摆脱 ObjectOutputStream
并使用写文本的东西,而不是对象(例如,PrintWriter
)。请注意,您可以使用您最喜欢的文本编辑器或 cat
或其他任何工具检查 JSON 文件,以查看它是否包含您期望的内容。
在稍后解析 JSON 方面,删除所有读入字符串的代码,创建一个 Reader
对象(例如,一个 InputStreamReader
),以及 pass that to fromJson()
.