How to create a new text file to write to in objec

2019-09-08 17:14发布

How would I go about creating a new text file in my program so that I can write content to it? I already understand how to write content but I haven't been able to find anything (anything easy to understand) on the subject.

3条回答
唯我独甜
2楼-- · 2019-09-08 17:19

You can use UITextView to write text

save the file using

     [txtView.text writeToFile:filePath atomically:NO  encoding:NSUTF8StringEncoding error:nil];

Get the TextFile content using

NSString* content = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
查看更多
做个烂人
3楼-- · 2019-09-08 17:32

Your question is so broad one could drive a truck (or lorry, in UK English) through the middle of it.

But in general, you could add a UITextView to one of your view controllers.

And when you are ready to save, you could take the contents of the text view (which is a NSString), and save it to a file via the NSString writeToFile methods.

And you can load the text view later on via NSString's "initFromFile" method, as long as you know the path to that file.

Here are other questions that people have asked that may help you out.

查看更多
Explosion°爆炸
4楼-- · 2019-09-08 17:42

Use this method:

+ (id)stringWithContentsOfFile:(NSString *)path
                  usedEncoding:(NSStringEncoding *)enc
                         error:(NSError **)error

An example:

NSString* path = [[NSBundle mainBundle] pathForResource:@"Example"
                                                 ofType:@"txt"];
NSError *error = nil;
NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];

if(error) 
{ 
    NSLog(@"ERROR while loading from file: %@", error);
}

Write to a file works this way: Use this method:

 - (BOOL)writeToFile:(NSString *)path 
          atomically:(BOOL)useAuxiliaryFile 
            encoding:(NSStringEncoding)enc 
               error:(NSError **)error

Example:

[text writeToFile:path atomically:NO  encoding:NSUTF8StringEncoding error:nil];

Mistake:

Can't just write to bundle path. Need to copy it to Documents Directory.

-(void)copyBundleToDocuments
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"Example.txt"];
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"Example.txt"];

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
        if (success) 
        {
            [text writeToFile:documentPlistPath atomically:NO  encoding:NSUTF8StringEncoding error:nil];
        }
}
查看更多
登录 后发表回答