【问题标题】:Equivalent for NSString writeToFile and archiveRootObject in Android ? [closed]等效于 Android 中的 NSString writeToFile 和 archiveRootObject ? [关闭]
【发布时间】:2013-08-21 14:14:00
【问题描述】:

在 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

【问题讨论】:

标签: java android ios objective-c writetofile


【解决方案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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    相关资源
    最近更新 更多