读取文件内容的三种方式:

  NSString *path = @"/Users/xiaogao/Desktop/path3";

    NSString *error;

    NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    if(error == nil){

        NSLog(@"读取文件成功:%@",str1);

    }else{

        NSLog(@"读取文件失败:%@",error);

    }

    

    NSURL *url = [NSURL URLWithString:@"file:///Users/xiaogao/Desktop/path3"];

    NSString *str2 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",str2);

    

    NSURL *url2 = [NSURL URLWithString:@"http://wwwibaidu.com"];

    NSString *str3 = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",str3);

 

写入文件:

  NSString *str = @"123456";

    NSString *path = @"/Users/xiaogao/Desktop/path4";

    NSError *error;

   //YES代表要进行原子性操作,也就是会创建一个中间的临时文件

    [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

    if(error){

        //显示主要信息

        NSLog(@"写入失败:%@",[error localizedDescription]);

    }else{

        NSLog(@"写入成功");

    }

相关文章:

  • 2021-12-15
  • 2021-11-30
  • 2021-06-16
  • 2022-01-17
  • 2022-01-16
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
相关资源
相似解决方案