SML*_*SML 10 string file-io android json
我正在写.json文件,我想读取该文件,但问题是,当我尝试将整个文件作为字符串读取时,它会在每个字符之前和之后添加空格,并且因为额外的字符,它无法读取json.
Json格式是
[{"description1":"The ThinkerA bronze sculpture by Auguste Rodin. It depicts a man in sober\nmeditation battling with a powerful internal struggle.","description2":"Steve JobsFounder of Apple, he is widely recognized as a charismatic pioneer of\nthe personal computer revolution.","description3":"Justin BieberBorn in 1994, the latest sensation in music industry with numerous\nawards in recent years."}]
Run Code Online (Sandbox Code Playgroud)
但它给出了如下的响应:[{"描述1":"他......
修剪额外的空间我引用了这个,但stil没有用: Java如何用字符串中的单个空格替换2个或更多空格并仅删除前导空格
即时通讯使用此代码
File folderPath = Environment.getExternalStorageDirectory();
File mypath=new File(folderPath, "description.json");
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(mypath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1)
{
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
String response = fileData.toString();
Run Code Online (Sandbox Code Playgroud)
"response"字符串包含奇怪的响应
所以有人可以帮助我吗?
写入文件,即时使用:
FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);
Run Code Online (Sandbox Code Playgroud)
Dip*_*iya 21
写下以下写入Json文件的方法,这params
是一个文件名,mJsonResponse
是一个服务器响应.
用于将文件创建到应用程序的内部存储器
public void mCreateAndSaveFile(String params, String mJsonResponse) {
try {
FileWriter file = new FileWriter("/data/data/" + getApplicationContext().getPackageName() + "/" + params);
file.write(mJsonResponse);
file.flush();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
对于从Json文件读取数据,这params
是文件名.
public void mReadJsonData(String params) {
try {
File f = new File("/data/data/" + getPackageName() + "/" + params);
FileInputStream is = new FileInputStream(f);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String mResponse = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
Pra*_*ani 17
我喜欢上面的回答和编辑:我只是喜欢分享,所以我分享了可能对其他人有用的东西.
在包中复制并粘贴以下类,并使用如下:
MyJSON.saveData(context, jsonData);
String json = MyJSON.getData(context);
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
/**
* Created by Pratik.
*/
public class MyJSON {
static String fileName = "myBlog.json";
public static void saveData(Context context, String mJsonResponse) {
try {
FileWriter file = new FileWriter(context.getFilesDir().getPath() + "/" + fileName);
file.write(mJsonResponse);
file.flush();
file.close();
} catch (IOException e) {
Log.e("TAG", "Error in Writing: " + e.getLocalizedMessage());
}
}
public static String getData(Context context) {
try {
File f = new File(context.getFilesDir().getPath() + "/" + fileName);
//check whether file exists
FileInputStream is = new FileInputStream(f);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer);
} catch (IOException e) {
Log.e("TAG", "Error in Reading: " + e.getLocalizedMessage());
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
writeChars 将每个字符写为两个字节。
http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChars(java.lang.String )
http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChar(int )
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28381 次 |
最近记录: |