oc文件基本读写及操作

代码:

#import <Foundation/Foundation.h>

//NSString 写文件
void stringWriteToFile(){
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test.txt"];
NSString *s = @"test";
[s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\nstring = %@",str); NSString *testtxt = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest.text = %@",testtxt);
} //NSArray 写文件
void arrayWriteToFile(){
NSArray *arr = @[@"a",@"b",@"b",@"c"];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test2.txt"];
[arr writeToFile:path atomically:YES]; NSArray *a = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"array = \n%@",a); NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest2.txt = \n%@",s);
} //NSDictionary 写文件
void dictionaryWriteToFile(){
NSDictionary *dic = @{@"a":@"",
@"b":@"",
@"c":@"",
@"d":@""};
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test3.txt"];
[dic writeToFile:path atomically:YES]; NSDictionary *d = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"\ndictionary = \n%@",d); NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest3.txt = \n%@",s);
} //atomically 是否原子级 即事务性写入
//NSNumber、NSDate、NSData都可以通过writeToFile写入文件,该文件为纯文本类型,如果将后缀名改为plist即为xcode属性列表文件 int main(int argc, const char * argv[]) { //该方法是创建一个实例,但是使的NSFileManager的单例模式将失去效果
NSFileManager *fm0 = [[NSFileManager alloc] init]; //defaultManager使用单例模式创建NSFileManager对象
NSFileManager *fm1 = [NSFileManager defaultManager]; NSFileManager *fm2 = [NSFileManager defaultManager]; NSLog(@"\nfm0 = %p,fm1 = %p,fm2 = %p",fm0,fm1,fm2); NSData *data = [[NSString stringWithFormat:@"main"] dataUsingEncoding:NSUTF8StringEncoding]; NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/main.txt"]; if (![fm1 fileExistsAtPath:path]) {
if ([fm1 createFileAtPath:path contents: data attributes:nil]) {
NSLog(@"create success");
NSDictionary *d = [fm1 attributesOfItemAtPath:path error:nil]; NSLog(@"\nattributesOfItemAtPath = \n%@",d); //NSFileSize是预定义的文件属性key,可通过查看系统文件获取其他属性key并通过
//以下方法获取其属性值
NSNumber *filesize = [d valueForKey:NSFileSize]; NSLog(@"\nfilesize = %@",filesize); //读文件
NSData *data1 = [fm1 contentsAtPath:path]; NSString *s = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding]; //文件系统的属性
//总空间,已用空间,可用空间,文件数量,
NSLog(@"\nattributesOfFileSystemForPath = \n%@",[fm1 attributesOfFileSystemForPath:path error:nil]); NSLog(@"\nmain.txt = %@",s);
}
}
else{ NSString *copypath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_copy.txt"];
[fm1 copyItemAtPath:path toPath:copypath error:nil]; //重命名可以目标路径与主路径一致但是文件名不同
NSString *movepath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_move.txt"];
[fm1 moveItemAtPath:path toPath:movepath error:nil]; //删除文件
if ([fm1 removeItemAtPath:path error:nil]) {
NSLog(@"remove %@ success",path);
}
if ([fm1 removeItemAtPath:copypath error:nil]) {
NSLog(@"remove %@ success",copypath);
}
if ([fm1 removeItemAtPath:movepath error:nil]) {
NSLog(@"remove %@ success",movepath);
}
} stringWriteToFile(); arrayWriteToFile(); dictionaryWriteToFile(); return ;
}

结果:

-- ::26.064 NSFileManagerDemo[:]
fm0 = 0x1001145d0,fm1 = 0x100114620,fm2 = 0x100114620
-- ::26.075 NSFileManagerDemo[:] remove /Users/yoran_yang/Documents/main_copy.txt success
-- ::26.076 NSFileManagerDemo[:] remove /Users/yoran_yang/Documents/main_move.txt success
-- ::26.076 NSFileManagerDemo[:]
string = test
-- ::26.077 NSFileManagerDemo[:]
test.text = test
-- ::26.078 NSFileManagerDemo[:] array =
(
a,
b,
b,
c
)
-- ::26.078 NSFileManagerDemo[:]
test2.txt =
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>a</string>
<string>b</string>
<string>b</string>
<string>c</string>
</array>
</plist>
-- ::26.079 NSFileManagerDemo[:]
dictionary =
{
a = ;
b = ;
c = ;
d = ;
}
-- ::26.079 NSFileManagerDemo[:]
test3.txt =
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>a</key>
<string></string>
<key>b</key>
<string></string>
<key>c</key>
<string></string>
<key>d</key>
<string></string>
</dict>
</plist>
Program ended with exit code:
上一篇:全数字锁相环(DPLL)的原理简介以及verilog设计代码


下一篇:spring-cloud构架微服务(2)-全局配置二