我加入的.txt文件,文檔文件夾中使用的解決方案建立在這裏存放了一些數據:所以現在 Read Text File in Document Folder - Iphone SDK清潔.TXT在Documents文件夾
我可以寫,並從該文件讀取成功地文字,但我也需要採取行動將清除此文件中的所有文本,使其完全空白。可能嗎?
我加入的.txt文件,文檔文件夾中使用的解決方案建立在這裏存放了一些數據:所以現在 Read Text File in Document Folder - Iphone SDK清潔.TXT在Documents文件夾
我可以寫,並從該文件讀取成功地文字,但我也需要採取行動將清除此文件中的所有文本,使其完全空白。可能嗎?
在一條線,這應該做到這一點:
[[NSData data] writeToFile: pathToFile atomically: YES];
這將循環中的所有文件-in文件目錄 - 其中有名爲「txt」後綴,並刪除其內容:
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
for (NSString *fileName in arr) {
if ([fileName hasSuffix:@".txt"])
{
[@"" writeToFile:[path stringByAppendingPathComponent:fileName] atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
}
從iOS 8開始,請不要**使用它來獲取Documents文件夾。它不再僅僅是「家」目錄。 – rmaddy
好,最簡單的方法是刪除文件,然後創建一個具有相同名稱的新文件。你可以使用[本教程](http://mobiledevelopertips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html)來學習如何做到這一點。 – Oren