12

我正在编写 .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."}]

但它给出了奇怪的响应,例如:[ {“description 1”:“T he .....

修剪多余的空格我提到了这个,但仍然没有工作: 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();

“响应”字符串包含奇怪的响应

那么有人可以帮助我吗?

对于写入文件,我使用:

FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);
4

4 回答 4

24

为写入 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();
    }
}

对于从 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();
    }
}
于 2013-01-09T06:03:24.493 回答
20

我喜欢上面的答案并编辑:我只是喜欢分享,所以我分享了可能对其他人有用的东西。

在您的包中复制并粘贴以下类并使用如下:

节省: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;
        }
    }
}
于 2015-11-06T17:57:17.620 回答
7

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.
于 2013-01-09T05:23:21.577 回答
1

您的编写代码是问题所在。只需使用

FileWriter fos = new FileWriter(mypath);
fos.write(response);
于 2013-01-09T05:35:31.557 回答