Saving text file to documents directory in iOS 7

后端 未结 3 341
死守一世寂寞
死守一世寂寞 2020-12-08 08:02

I am trying to save a plain text file to the Documents directory in iOS 7. Here is my code:

//Saving file
NSFileManager *fileManager = [[NSFileManager alloc]         


        
相关标签:
3条回答
  • 2020-12-08 08:33

    Mundi's answer in Swift:

        let fileName = "/File Name.txt"
        let filePath = self.applicationDocumentsDirectory().path?.stringByAppendingString(fileName)
    
        do {
            try strFileContents.writeToFile(filePath!, atomically: true, encoding: NSUTF8StringEncoding)
            print(filePath)
        }
        catch {
            // error saving file
        }
    
    func applicationDocumentsDirectory() -> NSURL {
    
        return NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!
    }
    
    0 讨论(0)
  • 2020-12-08 08:35

    From Apple's Xcode Template:

    /**
     Returns the URL to the application's Documents directory.
     */
    - (NSURL *)applicationDocumentsDirectory {
        return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory 
             inDomains:NSUserDomainMask] lastObject];
    }
    

    You can save like this:

    NSString *path = [[self applicationDocumentsDirectory].path 
                           stringByAppendingPathComponent:@"fileName.txt"];
    [sampleText writeToFile:path atomically:YES
                           encoding:NSUTF8StringEncoding error:nil];
    
    0 讨论(0)
  • 2020-12-08 08:40
        -(void)writeATEndOfFile:(NSString *)content2
        {
            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:@"%@/textfile.txt",
                                  documentsDirectory];
    
            if([[NSFileManager defaultManager] fileExistsAtPath:fileName])
            {
                NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:fileName];
                [fileHandle seekToEndOfFile];
                NSString *writedStr = [[NSString alloc]initWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
                content2 = [content2 stringByAppendingString:@"\n"];
                writedStr = [writedStr stringByAppendingString:content2];
    
                [writedStr writeToFile:fileName
                            atomically:NO
                              encoding:NSStringEncodingConversionAllowLossy
                                 error:nil];
            }
            else {
                int n = [content2 intValue];
                [self writeToTextFile:n];
            }
    
        }   
    
    
     -(void) writeToTextFile:(int) value{
            //get the documents directory:
            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:@"%@/textfile.txt",
                                  documentsDirectory];
            //create content - four lines of text
           // NSString *content = @"One\nTwo\nThree\nFour\nFive";
    
            NSString *content2 = [NSString stringWithFormat:@"%d",value];
            content = [content2 stringByAppendingString:@"\n"];
            //save content to the documents directory
            [content writeToFile:fileName
                      atomically:NO
                        encoding:NSStringEncodingConversionAllowLossy
                           error:nil];
    
        }
    
    0 讨论(0)
提交回复
热议问题