从内部存储中的URL保存JSON文件

时间:2013-10-11 09:57:12

标签: java android

对于我的Android应用程序,我需要从网址下载一个JSON到android的内部存储,然后从中读取。我认为将它作为byte []保存到内部存储中的最佳方法虽然我在这里遇到了一些问题但是到目前为止我写的是

File storage = new File("/sdcard/appData/photos");
storage.mkdirs();

JSONParser jParser = new JSONParser();

// getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url1);

//transforming jsonObject to byte[] and store it
String jsonString = json.toString();                
byte[] jsonArray = jsonString.getBytes();

String filen = "jsonData";
File fileToSaveJson = new File("/sdcard/appData",filen);

FileOutputStream fos;
fos = new FileOutputStream(fileToSaveJson);

fos = openFileOutput(filen,Context.MODE_PRIVATE);
fos.write(jsonArray);
fos.close();


//reading jsonString from storage and transform it into jsonObject              
FileInputStream fis;
File readFromJson = new File("/sdcard/appData/jsonData");

fis =  new FileInputStream(readFromJson);

fis =  new FileInputStream(readFromJson);
InputStreamReader isr = new InputStreamReader(fis);

fis.read(new byte[(int)readFromJson.length()]);

但它不会打开文件以便阅读

3 个答案:

答案 0 :(得分:1)

public static File createCacheFile(Context context, String fileName, String json) {
    File cacheFile = new File(context.getFilesDir(), fileName);
    try {
        FileWriter fw = new FileWriter(cacheFile);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();

        // on exception null will be returned
        cacheFile = null;
    }

    return cacheFile;
}

public static String readFile(File file) {
    String fileContent = "";
    try {
        String currentLine;
        BufferedReader br = new BufferedReader(new FileReader(file));

        while ((currentLine = br.readLine()) != null) {
            fileContent += currentLine + '\n';
        }

        br.close();
    } catch (IOException e) {
        e.printStackTrace();

        // on exception null will be returned
        fileContent = null;
    }
    return fileContent;
}

答案 1 :(得分:1)

  private void downloadAndStoreJson(String url,String tag){

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(url);          

        String jsonString = json.toString();                
        byte[] jsonArray = jsonString.getBytes();

        File fileToSaveJson = new File("/sdcard/appData/LocalJson/",tag);



        BufferedOutputStream bos;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(fileToSaveJson));
            bos.write(jsonArray);
            bos.flush();
            bos.close();

        } catch (FileNotFoundException e4) {
            // TODO Auto-generated catch block
            e4.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            jsonArray=null;
            jParser=null;
            System.gc();
        }

  }

答案 2 :(得分:0)

public void writeObjectInInternalStorage(Context context, String filename, Object object) throws IOException {
    FileOutputStream fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
    objectOutputStream.writeObject(object);
    objectOutputStream.close();
    fileOutputStream.close();
}

public Object readObjectFromInternalStorage(Context context, String filename) throws IOException, FileNotFoundException, ClassNotFoundException{
    FileInputStream fileInputStream = context.openFileInput(filename);
    return new ObjectInputStream(fileInputStream).readObject();
}

使用这些方法,致电readObjectFromInternalStorage()并将其投放到JSON String