开发者

iPhone Saving File

to every one, i am doing xml parsing and i am getting two text files from the server with contents i have stored the contents of the first file document directory开发者_开发知识库 however when parsing function call second time the contents are overwritten by second file, i don't know how to create two txt file in document directory for two different contents

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Report.txt"];
        [text writeToFile:documentsDirectory atomically:NO];
        [text release];

I have taken the content for the first file with this method.


You simply need to provide a different filename each time.

i.e.: In the...

documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Report.txt"];

...line, specifying something other than "Report.txt".


This is a function that you can use to find a unique path where to save a file in the Documents directory.

You can call this each time you want a new unique path.

- (NSString *)findUniqueSavePath{

    int i = 1;
    NSString *path;
    do {
        // iterate until a name does not match an existing file
        path = [NSString stringWithFormat:@"%@/Documents/txt_file_%03d.txt", NSHomeDirectory(), i++];
    } while ([[NSFileManager defaultManager] fileExistsAtPath:path]);

    return path;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜