Objective-c 文件的基本操作
来源:互联网 发布:淘宝 排名 编辑:程序博客网 时间:2024/05/22 08:39
保证testfile文件在程序运行的目录,这个目录与程序编写目录不一致,可以使用NSFileManager实例的currentDirectoryPath查看
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){ @autoreleasepool { <span style="white-space:pre"></span> NSString *fName = @"testfile"; NSFileManager *fm ; NSDictionary *attr; //创建文件管理的实例 fm = [NSFileManager defaultManager]; //打印当前目录,这个才是程序运行的目录, NSLog(@"%@",[fm currentDirectoryPath]); //文件是否存在 if ([fm fileExistsAtPath:fName] == NO) { NSLog(@"File doesn't exist!"); } //复制文件为 newfile. if([fm copyItemAtPath:fName toPath:@"newfile" error:NULL]==NO){ NSLog(@"File Copy failed!"); } // 测试两个文件是否一致 if ([fm contentsEqualAtPath:fName andPath:@"newfile"]==NO){ NSLog(@"File are Not Equal!"); } //重命名副本 if ([fm moveItemAtPath:@"newfile" toPath:@"newfile2" error:NULL] ==NO) { NSLog(@"File rename Failed"); } //获取文件属性 if ((attr = [fm attributesOfItemAtPath:fName error:NULL]) ==nil) { NSLog(@"Could't get file attributes!"); } //打印文件属性中大小 NSLog(@"File size is %llu bytes",[[attr objectForKey:NSFileSize] unsignedLongLongValue]); //删除原始文件 if ([fm removeItemAtPath:fName error:NULL] ==NO) { NSLog(@"File remove failed"); } //显示新创建的内容 NSLog(@"%@",[NSString stringWithContentsOfFile:@"newfile2" encoding:NSUTF8StringEncoding error:NULL]); } return 0;}
运行结果:
/Users/wudi/Library/Developer/Xcode/DerivedData/prog16.1-eraeaurndadvfjcvyqzecysfsbpx/Build/Products/Debug
File size is 88 bytes
This is a test file with some data in it.
Here's another line of the data.
And a third.
0 0
- Objective-c 文件的基本操作
- Objective-c语言_文件基本操作
- Objective-C 文件操作
- Objective-C 文件操作
- Objective-c文件操作
- objective-c 字符串基本操作
- Objective-C基本数据类型操作
- C++---文件的基本操作
- Objective-C入门02:一些简单的基本操作
- Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作
- Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作
- Objective-C文件流操作
- Objective-C的基本语法
- 文件的基本操作 C语言版
- C中访问文件的基本操作
- linux c 文件的基本操作
- C语言的基本文件操作
- c/c++文件的基本操作
- 在汉澳sinox2014建立ZFS高可靠文件存储系统
- HDU 4951
- windows编程--动态加载光标和图标
- JSON学习
- PHP注释以及各种语言的注释
- Objective-c 文件的基本操作
- VisualGDB调试
- Android权限之sharedUserId和签名
- javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
- 学习资源汇总
- 网络\路由器 端口映射的作用
- css3 box-sizing属性
- LeetCode 014 Longest Common Prefix
- 如何完全备份android系统上的system分区和data分区