Эквивалент для NSString writeToFile и archiveRootObject в Android?

На iOS я использую эти строки кода для хранения большой строки (JSon)... Как я могу сделать то же самое на Android? В каком месте файла поместить эту строку? Спасибо

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];


//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];

А для сохранения непосредственно массива? Как это?

   fullFileName = [NSString stringWithFormat:@"%@/specialElements", documentsDirectory];
  [NSKeyedArchiver archiveRootObject:specialElements toFile:fullFileName];

specialElements - это NSMutableArray

1 ответ

Решение
try
{
    String path = "/data/data/"+getPackageName()+"/test.json";
    File file = getFileStreamPath(path); 
    if (!file.exists())
    {
      file.createNewFile();
    }

    FileOutputStream writer = openFileOutput(file.getName(), Context.MODE_PRIVATE);
    writer.write(data.getBytes());//data is your json in String
    writer.flush();
    writer.close();
}
catch(Exception e)
{
    //exception
}

Вместе с указанным ниже разрешением в файле манифеста:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Другие вопросы по тегам