Read Text File in Document Folder - Iphone SDK

后端 未结 3 1533
情深已故
情深已故 2020-12-19 10:37

I have this code below:

    NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@\"recentDownload\"];
    NSString *fullPath = [NSBundle         


        
相关标签:
3条回答
  • 2020-12-19 11:20
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
    
        NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
    
        if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
        {
            NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
        }       
    
        //Load from File
    NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];
    

    This worked for me

    Anyway, thank you all..

    0 讨论(0)
  • 2020-12-19 11:34

    For read/write from text file check this url.

    0 讨论(0)
  • 2020-12-19 11:38

    The NSBundle class is used for finding things within your applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work. Try this instead:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask,
                                                         YES);
    
    NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"]; 
    
    0 讨论(0)
提交回复
热议问题