您的位置:首页 > 移动开发 > Objective-C

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: