因此,我正在开发读取包含一些数据的JSON文本文件的android应用程序。我在文本文件(here)中有一个300 kb(307,312字节)的JSON。我还开发桌面应用程序(cpp)来生成和加载(和解析)JSON文本文件。不同数量的字符在Java Android InputStream和C++ ifstream中
当我尝试在C++中使用ifstream打开并阅读它时,我得到正确的字符串长度(307,312)。我甚至成功解析它。
这是我在C++代码:
std::string json = "";
std::string line;
std::ifstream myfile(textfile.txt);
if(myfile.is_open()){
while(std::getline(myfile, line)){
json += line;
json.push_back('\n');
}
json.pop_back(); // pop back the last '\n'
myfile.close();
}else{
std::cout << "Unable to open file";
}
在我的Android应用程序,我把在res /我JSON文本文件的原始文件夹。当我尝试打开并使用InputStream读取时,字符串的长度只有291,896。我无法解析它(我使用相同的C++代码使用jni解析它,也许它不重要)。
InputStream is = getResources().openRawResource(R.raw.textfile);
byte[] b = new byte[is.available()];
is.read(b);
in_str = new String(b);
UPDATE:
我也有尝试使用this方式。
InputStream is = getResources().openRawResource(R.raw.textfile);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while(line != null){
in_str += line;
in_str += '\n';
line = reader.readLine();
}
if (in_str != null && in_str.length() > 0) {
in_str = in_str.substring(0, in_str.length()-1);
}
即使我试着将它从res/raw文件夹移动到java android项目中的assets文件夹。当然,我将InputStream
行更改为InputStream is = getAssets().open("textfile.txt")
。还是行不通。