writing .json file and read that file in Android

前端 未结 4 1095
孤城傲影
孤城傲影 2020-12-30 08:43

i m writing .json file and i want to read that file, but the problem is, when i try to read whole file as string it adds the space before and after every character and just

4条回答
  •  孤城傲影
    2020-12-30 09:15

    Write below method for Write Json File, Here params is a File Name and mJsonResponse is a Server Response.

    For Create Files into Internal Memory of Application

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

    For Read Data From Json File, Here params is File Name.

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

提交回复
热议问题