Objective-C Foundation框架实践——NSString(四)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
  @autoreleasepool {
    
    //////////////////////////
    
    //读取文件;
    //1.本地文件;  2.网络文件;
    //路径类
    NSString *string11 = @"http://www.baidu.com";
    
    //网络路径;应该根据这两种方法去读取;
    NSURL *httpUrl = [NSURL URLWithString:string11];
    
    //本地路径;
    NSURL *localUrl = [NSURL fileURLWithPath:string11];
    
    
    NSString *httpStr = [NSString stringWithContentsOfURL:httpUrl encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"httpStr = %@",httpStr);
    
    NSString *localStr = [NSString stringWithContentsOfFile:@"/Users/chenyufeng/XcodeWorkspace/NSStringDemo/NSStringDemo/readme.txt" encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"localStr = %@",localStr);
    
    
    //写入文件
    NSString *string12 = @"这是需要写入的内容";
    [string12 writeToFile:@"/Users/chenyufeng/XcodeWorkspace/NSStringDemo/NSStringDemo/readme.txt" atomically:true encoding:NSUTF8StringEncoding error:nil];
    
    
    
    
    
    
    return 0;
  }
}


以上方法就可以实现读取文件和写入文件。


github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

你可能感兴趣的:(Objective-C,Foundation)