Objective-C 文件操作
2015-06-07 00:00
656 查看
// // main.m // KenshinCui // // Created by hxd_mac on 15-6-7. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> void Test1() { //读取本地文档 NSString *path = @"/Users/hxd_mac/Documents/developer/KenshinCui/test.txt"; NSString *str1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSLog(@"str1 = %@", str1); NSError *error; NSString *str2 = [NSString stringWithContentsOfFile:path encoding:kCFStringEncodingGB_18030_2000 error:&error]; //错误处理 if (error) { NSLog(@"read error ,the error is %@", error); } else { NSLog(@"read success, the file contens of is %@", str2); } //读取网络文档 // NSURL *url = [NSURL URLWithString:@"http://www.apple.com/"]; // // NSString *str3 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; // // NSLog(@"str3 = %@", str3); } void Test2() { //文件写入 NSString *path = @"/Users/hxd_mac/Documents/developer/KenshinCui/test2.txt"; NSError *error; NSString *string = @"hello world!"; [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (error) { NSLog(@"write fail!"); } else { NSLog(@"write success %@", string); } } void Test3() { //路径操作 NSMutableArray *path = [NSMutableArray array]; [path addObject:@"OC"]; [path addObject:@"apple"]; [path addObject:@"swift"]; NSString *string = [NSString pathWithComponents:path]; NSLog(@"string: %@", string); NSLog(@"path: %@", [string pathComponents]); NSLog(@"isAbsolution path %i", [string isAbsolutePath]); NSLog(@"the last object: %@", [string lastPathComponent]); NSLog(@"after append: %@", [string stringByAppendingPathComponent:@"iphone"]); NSLog(@"after delete: %@", [string stringByDeletingLastPathComponent]); } void Test4() { //扩展名操作 NSString *path = @"/Users/hxd_mac/Documents/developer/KenshinCui/test2.txt"; NSLog(@"find the extend: %@", [path pathExtension]); NSLog(@"after delete: %@", [path stringByDeletingPathExtension]); NSLog(@"after append: %@", [path stringByAppendingPathExtension:@"mp3"]); } int main(int argc, const char * argv[]) { @autoreleasepool { Test1(); Test2(); Test3(); Test4(); } return 0; }
输出结果:
2015-06-07 16:15:21.525 KenshinCui[1623:303] str1 = 世界你好
2015-06-07 16:15:21.644 KenshinCui[1623:303] read error ,the error is Error Domain=NSCocoaErrorDomain Code=261 "The file couldn’t be opened using the specified text encoding." UserInfo=0x10010f6a0 {NSFilePath=/Users/hxd_mac/Documents/developer/KenshinCui/test.txt, NSStringEncoding=1586}
2015-06-07 16:15:21.649 KenshinCui[1623:303] write fail!
2015-06-07 16:15:21.651 KenshinCui[1623:303] string: OC/apple/swift
2015-06-07 16:15:21.653 KenshinCui[1623:303] path: (
OC,
apple,
swift
)
2015-06-07 16:15:21.658 KenshinCui[1623:303] isAbsolution path 0
2015-06-07 16:15:21.660 KenshinCui[1623:303] the last object: swift
2015-06-07 16:15:21.662 KenshinCui[1623:303] after append: OC/apple/swift/iphone
2015-06-07 16:15:21.666 KenshinCui[1623:303] after delete: OC/apple
2015-06-07 16:15:21.668 KenshinCui[1623:303] find the extend: txt
2015-06-07 16:15:21.670 KenshinCui[1623:303] after delete: /Users/hxd_mac/Documents/developer/KenshinCui/test2
2015-06-07 16:15:21.671 KenshinCui[1623:303] after append: /Users/hxd_mac/Documents/developer/KenshinCui/test2.txt.mp3
相关文章推荐
- [C++/object c]_[初级]_[std::map容器的使用总结和NSDictionary词典使用总结]
- 使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash
- 基于json-lib.jar包 创建JSONObject的四个常用方法
- ComFailException:can't co-create object.错误的猜测的一种解决方法
- WaitForMultipleObjects使用详解
- 十分钟让你明白Objective-C的语法(和Java、C++的对比)
- Different ways to create an object in Java
- 浏览器对象模型(Browser Object Model)
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]
- 轻松学习之一--Objective-C消息转发
- Object.prototype.toString.call() 区分对象类型
- Objective-C的入门(一)
- Object-c分类和协议
- Objective-c的内存管理MRC与ARC
- Delphi 中的 procedure of object .
- object-c面向对象1
- Objective-C 2.0属性(Property) retain,copy,assign,copy
- NSObject和反射
- java中Array/List/Map/Object与Json互相转换详解
- Object-C-Foundation-数组排序