OC中读写文件到本地

来源:互联网 发布:神盾网络验证山寨 编辑:程序博客网 时间:2024/06/11 03:05


//写文件

        //        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

        NSMutableString *mystring = [[NSMutableStringalloc] init];

        NSError *err;

        for(int i =0; i < 10; i++){

            [mystring appendString:@"This is for reading file .\n"];

            [mystring writeToFile:@"/tmpa/myfile2.txt"

                       atomically:YES

                         encoding:NSUTF8StringEncoding

                            error:&err];


        }

        NSLog(@"Yes, the file is writen done with %@", [errlocalizedDescription]);

//读文件

        NSError *readerr;

        NSString *str = [[NSStringalloc] initWithContentsOfFile:@"/tmp/myfile2.txt"

    encoding:NSUTF8StringEncoding

                                                           error:&readerr];

        if(!str){

            NSLog(@"Read file with Error:%@", [readerrlocalizedDescription]);

        }else{

            NSLog(@"The file has the contents as below: %@", str);

        }


原创粉丝点击