文件读写

#import

intmain(intargc,constchar* argv[])

{

    @autoreleasepool

    {

        NSFileManager *manager = [[NSFileManager alloc]init];

        NSString *path = @"/Users/qijiaming/Desktop/1410C.txt";


        //加入内容

        NSString*str =@"wo ai ni ";

        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];


        [managercreateFileAtPath:path contents:data attributes:nil];


        //获取路径

        NSString *lu = [path stringByAppendingPathComponent:@"1410C.txt"];

        NSLog(@"%@",lu);


        //先打开

        NSFileHandle*handle = [NSFileHandlefileHandleForUpdatingAtPath:path];


        NSData*du = [handlereadDataToEndOfFile];//在读


        NSString *huan = [[NSString alloc]initWithData:du encoding:NSUTF8StringEncoding];//转换成 二进制


        //存到字典并输出

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:huan,@"值", nil];

        NSLog(@"%@",dic.allValues);


       //判断

        NSFileManager*manager1 = [[NSFileManageralloc]init];

        NSString *path1 =@"/Users/qijiaming/Desktop/love10C.txt";

        BOOLa = [manager1fileExistsAtPath:path1];

        if(a ==1)

        {

            NSLog(@"存在");

        }

        else

        {

            [managercreateFileAtPath:path1 contents:nil attributes:nil];

        }


        //写入内容

        NSFileHandle*handle1 = [NSFileHandlefileHandleForUpdatingAtPath:path1];

        NSString *str1 = @"我是1602C班的成员,我们是一个集体";

        NSData *data1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];

        [handle1writeData:data1];

        [handle1seekToFileOffset:0];



        //读取内容

        NSData*dateNew = [handle1readDataToEndOfFile];

        NSString *strNew = [[NSString alloc]initWithData:dateNew encoding:NSUTF8StringEncoding];

        NSLog(@"%@",strNew);



    }

    return 0;

}

你可能感兴趣的:(文件读写)