我是一名新的iOS開發人員。我想創建一個應用程序,它會在應用程序的文件夾中自動創建新的* .txt文件。我只能找到如何打開,關閉,保存,寫入和讀取 - 而不是創建。我如何創建新文件?謝謝。如何在iOS上以編程方式創建.txt文件
2
A
回答
13
使用以下代碼在應用程序的Documents目錄中編寫/創建.txt文件。它應該是不言自明的。
NSError *error;
NSString *stringToWrite = @"1\n2\n3\n4";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myfile.txt"];
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
,並檢索文本文件:
NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", str);
4
我希望這個幫的你
-(void)writeToTextFile{
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 *fileName = [NSString stringWithFormat:@"%@/filename.txt",
documentsDirectory];
NSString *content = @"This is Demo";
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
}
-(void)ShowContentlist{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/filename.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
[content release];
}
+0
它運行正常,謝謝 – kemdo 2014-09-30 05:01:07
相關問題
- 1. 在iOS中以編程方式創建AAC文件?
- 2. 以編程方式創建NSObjects ios
- 3. 以編程方式創建ZIP文件
- 4. 以編程方式創建.pfx文件
- 5. 以編程方式創建designer.cs文件
- 6. 如何在iOS中以編程方式創建PDF
- 7. 如何以編程方式在Sharepoint站點創建文件夾
- 8. 如何以編程方式在iphone中創建csv文件?
- 9. 如何在CMD上創建.txt文件?
- 10. 如何以編程方式創建新的MODx上下文?
- 11. 如何以編程方式創建UIScrollview
- 12. 如何以編程方式創建UIViewController?
- 13. 如何以編程方式創建UIButton
- 14. 如何以編程方式創建NSTextField?
- 15. 如何以編程方式創建Tabbar
- 16. 如何以編程方式創建ProgressBar?
- 17. 如何以編程方式創建CustomCell?
- 18. 如何以編程方式創建NSPopover
- 19. 如何以編程方式創建GPO?
- 20. 如何以編程方式創建NSFetchedPropertyDescription?
- 21. 如何以編程方式創建UIScrollView?
- 22. 如何以編程方式創建TextView?
- 23. 如何以編程方式創建NSSearchField?
- 24. 以編程方式在C#中創建KMZ文件KML文件
- 25. 如何以編程方式在Google文檔中創建文檔?
- 26. 如何以編程方式在rails中創建上傳文件的實例
- 27. 如何在Galaxy Nexus上以編程方式存儲創建的文件?
- 28. 如何以編程方式創建KMZ文件?
- 29. 如何以編程方式創建佈局文件
- 30. 如何以編程方式創建NDepend項目文件?
這是因爲寫作和創造一個步驟發生 – borrrden 2014-09-30 04:18:33