网络连接,文件的读写

来源:互联网 发布:h3c路由器端口映射 sql 编辑:程序博客网 时间:2024/06/03 11:08

文件的读

文件的读取非常方便

NSString类

initWithContentsOfFile:encoding: error

stringWithContentsOfFile:encoding: error:

writeToFile: atomically:encoding: error: 


1首先创建文件路径(本项目以内的)

     NSString*bundlePath=nil;

     bundlePath=[[NSBundle mainBundlepathForResource:@"Read"ofType:@""];

2读取文件zhong中内容

    NSError*error;

    NSString*readerStr= [NSString stringWithContentsOfFile:bundlePath 

                                                                            encoding:NSUTF8StringEncoding

                                                                                   error:&error];

    NSLog(@"%@",readerStr);

文件的写

1  获取路径

NSString*writeFile=@"/Users/ibokan/Desktop/abc.txt";

2 判断是否xi写入成功,如果成功则输出1

 BOOLs=[readerStr writeToFile:writeFile

                atomically:YES

                  encoding:NSUTF8StringEncoding

                     error:&error];

    if(s) {

        NSLog(@"%@",[error description]);

    }

    NSLog(@"%d",s);


问题:如果传过来的shi是图片等东西呢?应该用什么来接收?

答:二进制流

文件的读取非常方便

字节数据缓冲类(byte buffer)

主要用于存储二进制

NSData

dataWithContentsOfFile:

initWithContentsOfFile: 

1创建路径

NSData*data=nil;

    data=[NSData dataWithContentsOfFile:bundlePath];

2读取数据

//此种方法不成功~原因在于格式没有被zhi指定为什么类型

    //NSString *str=[NSString stringWithFormat:@"%@",data];

    NSString*str= [[NSStringalloc]initWithData:data encoding:4];

    NSLog(@"%@",str);

3写入数据

[data writeToFile:writeFile atomically:YES];

//文件管理类

    NSFileManager*manger=[NSFileManager defaultManager];

    NSString*targetPath=@"/Users/ibokan/Desktop/ios43/abc";

    //如果创建成功或者已经存在返回YES

    BOOL s1= [manger createDirectoryAtPath:targetPath

                        withIntermediateDirectories:YES

                                                   attributes:nil

                                                          error:&error];

    NSLog(@"   s1=%d",s1);

    //创建目录

     NSString*targetPath1=@"/Users/ibokan/Desktop/ios43/abc/abc.txt";

    //NSData *data1=[NSData dataWithContentsOfFile:bundlePath];

    BOOLs2=[manger createFileAtPath:targetPath1 ];

    NSLog(@"s2=%d",s2);

HTTP请求方式

GET 请求。 POST 请求。

两种方式的区别

GET 通常用于请求URL并得到资源。

POST 用于向服务器添加新的内容。 

URL连接方式

同步连接。 异步连接。

两种方式的主要区别

同步连接时,UI“冻结”。 异步连接时,UI不冻结 

同步连接

同步连接是iOS中网络连接的一种方式,当请求的数据量小时,同步连接是一个很好的选 择。

UI“冻结”是它的一个缺点。 GET和POST两种请求都可以发起同步连接。 

同步创建一个同步请求

***************************以下是个人所写同步,异步的代码********************

同步:


 /*
    NSString *str1 = [NSString stringWithContentsOfURL:url
                                              encoding:4
                                                 error:nil];
    NSLog(@"str1 = %@",str1);
     */
    
    /*
    NSURLRequest *req = nil;
    req=[NSURLRequest requestWithURL:url
                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                     timeoutInterval:20.0f];
    NSHTTPURLResponse *resp=nil;
    NSError *error = nil;
    NSData *data=[NSURLConnection sendSynchronousRequest:req
                          returningResponse:&resp
                                      error:&error];

    if (resp)
    {
        NSLog(@"responseCode:%d",[resp statusCode]);
    }
    
    NSString *str1 = [[NSString alloc] initWithData:data
                                           encoding:4];
    NSLog(@"str1 = %@",str1);
    
    
    
    */
    
    NSURL * url = [NSURL URLWithString:@"http://best50.cn:8080/php/new_iphone.php"];
    //创建一个可变的请求
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
   //将请求方式设置为POST
    //?req=2&id=13
    
    [request setValue:@"2" forKey:@"req"];
    [request setValue:@"13" forKey:@"id"];
    
    [request setHTTPMethod:@"POST"];
    //设置回应和错误的指针变量
    NSHTTPURLResponse * response = nil;
    NSError * error = nil;
    //发起同步请求
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    //打印回应和错误信息
    NSLog(@"response = %@",[NSHTTPURLResponse localizedStringForStatusCode: [response statusCode]]);
    NSLog(@"error = %@",[error localizedDescription]);
    //打印返回的字符串信息
    NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"str = %@",str);
*********************异步*********************************
   //初始化一个URL
    NSURL *url = [NSURL URLWithString:@"http://www.oschina.net/ios/242/objective-c"];
    //创建一个请求
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    //发起异步连接
    //[NSURLConnection connectionWithRequest:request delegate:self];
    NSURLConnection *con = nil;
    con = [[NSURLConnection alloc] initWithRequest:request
                                          delegate:self
                                  startImmediately:YES];
    [con start];
   //其他的事情交给代理方法去完成
    NSLog(@"NSURLConnection 完成");
    
    return YES;
}



- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
    //NSLog(@"connection = %@",connection);
    NSLog(@"%s",__func__);
    _data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data
{
    //NSLog(@"connection = %@",connection);
    NSLog(@"%s",__func__);
    [_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //NSLog(@"connection = %@",connection);
    NSLog(@"%s",__func__);
    NSString *str = [[NSString alloc] initWithData:_data
                                          encoding:4];
    NSLog(@"str= %@",str);
    //[str release];
    //[_data release];
}

-(void)connection:(NSURLConnection *)connection
 didFailWithError:(NSError *)error
{
    //_data  = nil;
}

0 0
原创粉丝点击