- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
ios 声称文件已写入,但更改从未实际保存,就好像它们保留在缓冲区中一样。我需要冲洗什么的吗?
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
NSLog(@"\n\nPath = \n%@", myFilePath);
NSString *myFileContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"\n\nContents = \n%@", myFileContents);
[@"This line will be written to file" writeToFile:myFilePath
atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *changedContents = [NSString stringWithContentsOfFile:myFilePath
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"\n\nChanged Contents = \n%@", changedContents);
}
输出:
Path =
/Users/hamzeh/Library/Application Support/iPhone Simulator/5.1/Applications/2B318687-A745-4CD9-85BA-52A01AB76F6E/savepersistentdata_tutorial.app/MyFile.txt
Contents =
This is a text file.
The file will be displayed on the iPhone.
That is all for now.
Changed Contents =
This line will be written to file
这是我每次运行时得到的输出,所以更改实际上并没有写入文件。
感谢您的帮助。
最佳答案
您不能编辑主包中的文件。您必须先将文件保存到应用程序文档文件夹,然后再进行任何更改。
这里有一些代码可能会解决您的问题:
首先将文本保存到您创建的目录中:
NSString *myFilePath = [[NSBundle mainBundle]
pathForResource:@"MyFile"
ofType:@"txt"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *directory = [NSString stringWithFormat:@"%@/Some_Directory", [paths objectAtIndex:0]];
// Check if the directory already exists
if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) {
// Directory does not exist so create it
[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
}
NSData *data = [[NSData dataWithContentsOfFile:myFilePath] retain];
NSString *filePath = [[directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", [myFilePath lastPathComponent]]] retain];
NSError *error = nil;
if (![data writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error]) {
[data writeToFile:filePath atomically:NO];
}
[data release];
[filePath release];
关于iphone - ios writeToFile 更改不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10671017/
我有一些代码将一些 PDFDocument 对象写入用户选择的目的地。这很好用。 也许其中一些文件(由用户选择)可能非常大(可能数百兆字节),现在我想知道是否有可能取消当前的 writeToFile:
我在我的代码中使用 NSDictionary 的 writeToFile 属性,我正在尝试将字典更新到文档目录,如下所示, NSDictionary *revisionDict = [NSDictio
func fetchFromInternet(){ let i = Int(DISPATCH_QUEUE_PRIORITY_HIGH.value) let que = dispatch
我有以下代码: NSString *errorStr = nil; NSData *plistData = [NSPropertyListSerialization dataFromPropertyL
我正在将一些数据写入我的文档目录,我想知道是否有一种方法可以判断我的 writeToFile 方法何时完成以及我正在创建的文件是否已完全创建。提前致谢 这是我正在调用的方法 我只是在寻找一种方法,无论
我不明白为什么它总是失败。它返回NO。我整个下午都在搜索这个问题,但没有任何解释为什么或如何解决。调用 [[NSFileManager defaultManager] isWritableFileAt
我有一个正在更改的 plist: NSString *finalPath = [path stringByAppendingPathComponent:@"KeyFrameFileByMovie.pl
每次我使用相机拍照,然后保存时,图像总是横向的。这意味着我的 Xib 中的 UIImageView 是错误的。它的肖像 - 这就是我想要和期望的。我可以通过将图像旋转 90 度来纠正此问题,但即使如此
我试图将一个数组(其中包含一些字典)保存到 plist 文件中,但失败了。我没有收到任何错误。我在代码中使用另一个数组执行了与上面完全相同的几行,并且有效。.我不明白为什么它不保存文件。 这是我保存文
只要方法调用发生在同一应用程序运行时,似乎 writeToFile 方法就会附加到当前存在的文件路径。这是真的? 是否有任何其他方法可以确保在应用程序 session 运行时附加到现有文件?我希望在新
我正在尝试用 swift 将内容写入文件 let jsonResult = try! NSJSONSerialization.JSONObjectWithData(response.data, o
在我的 iPhone 应用程序中,我正在从网络上下载一些图像。它是否阻塞 UI 线程并不重要,事实上它需要阻塞 UI 线程直到完全下载。完成后,我通知 UI 唤醒并显示它们。 我的(简化的)代码是这样
我正在将我的 UITextView 文本保存到文件 test.txt 中 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocument
// directoryPath is a URL from another VC @IBAction func saveButtonTapped(sender: AnyObject) {
我目前正在编写一个简单的 IOS 应用程序,它将任务保存到表中。我想扩展该应用程序并允许人们与其他人共享“列表”。在将其保存到 XML 或 URL 之前,我想使用 writeToFile:atomic
我已成功使用 Plist 在我的 iPhone 应用程序中读取和写入数据。 Plist 由数组中的字典组成。当我想附加字典时,我使用 writeToFile 方法。这很有效,但它会覆盖整个 Plist
每次按下保存按钮时,我都会尝试将图像保存到磁盘。这是我正在使用的代码: NSArray* documentDirectories = NSSearchPathForDirectoriesInDomai
我使用有效的字典运行以下代码,但仍然“未写入”: NSFileManager* fm = [[NSFileManager alloc] init]; __autoreleasing NSError
我将新收到的 PDF 文档的缩略图呈现到文档目录中。 我正在使用以下代码: CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundl
ios 声称文件已写入,但更改从未实际保存,就好像它们保留在缓冲区中一样。我需要冲洗什么的吗? - (void)viewDidLoad { [super viewDidLoad]; N
我是一名优秀的程序员,十分优秀!