Foundation 框架 NSFileManager,NSData 简单的文件操作
一、简单展示NSFileManager的使用
#import <Foundation/Foundation.h> int main(int argc, const char * argv[])
{ @autoreleasepool {
//创建文件管理对象
NSFileManager *fm = [NSFileManager defaultManager];
//要操作的文件名
NSString *fname = @"myfile";
//获取文件的字典
NSDictionary *attr;
//当前路径
NSString *path;
//获取当前路径
path = [fm currentDirectoryPath];
//NSLog(@"\nThe current path is : %@", path); //检测文件是否存在
if ([fm fileExistsAtPath: fname] == NO) {
//如果不存在则建立一个文件
[fm createFileAtPath: fname contents: NULL attributes:nil];
//NSLog(@"\nThe file is not exist!");
//return 0;
}
//拷贝创建一个新文件, 新文件若已存在则报错
if ([fm copyItemAtPath: fname toPath: @"newFile" error: NULL] == NO) {
NSLog(@"\n Can't copy the file");
return ;
}
//检测两个文件内容是否相同
if ([fm contentsEqualAtPath: fname andPath: @"newFile"] == NO) {
NSLog(@"\nThe contents is not same");
return ;
}
//移动或者改名文件
if ([fm moveItemAtPath: @"newFile" toPath: @"myFile2" error:NULL] == NO) {
NSLog(@"\nCan't change the name");
return ;
}
//获取文件数据字典
if ((attr = [fm attributesOfItemAtPath: fname error:NULL]) == nil) {
NSLog(@"\nGet attributets failed");
return ;
}
//文件大小
NSLog(@"%@", attr[NSFileSize]);
//文件类型
NSLog(@"%@", attr[NSFileType]);
//创建者
NSLog(@"%@", attr[NSFileOwnerAccountName]);
//
NSLog(@"%@", attr[NSFileCreationDate]);
//显示文件内容
NSLog(@"\n Show the file contents");
NSLog(@"\n%@", [NSString stringWithContentsOfFile: fname encoding:NSUTF8StringEncoding error:NULL]);
}
return ;
}
二、通过NSData完成副本制作
int main(int argc, const char * argv[])
{ @autoreleasepool {
//通过NSDate来完成文件副本制作
NSFileManager *fm = [NSFileManager defaultManager];
NSData *dt; dt = [fm contentsAtPath: @"myfile"]; if (dt == nil) {
NSLog(@"Read file failed....");
return ;
} //将缓冲区NSData中的内容复制到文件中
if ([fm createFileAtPath:@"myFavoriteFile" contents: dt attributes:nil] == NO) {
NSLog(@"Creat backups failed");
return ;
} //读出文件内容
NSLog(@"\n%@", [NSString stringWithContentsOfFile:@"myFavoriteFile" encoding: NSUTF8StringEncoding error:NULL]);
}
return ;
}
三、简单的目录操作
#import <Foundation/Foundation.h> int main(int argc, const char * argv[])
{ @autoreleasepool {
NSString *newDir = @"newDir";
NSString *currentPath;
NSFileManager *fm = [NSFileManager defaultManager]; //获取当前路径
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath); //在当前目录下新建一个目录
if ([fm createDirectoryAtPath:newDir withIntermediateDirectories:TRUE attributes:nil error:NULL] == NO) {
NSLog(@"\nCouldn't creat the directory...");
return ;
} //更改路径名
if ([fm moveItemAtPath: newDir toPath: @"changeDir" error:NULL] == NO) {
NSLog(@"\nChange directory name failed");
return ;
} //更改当前路径
if ([fm changeCurrentDirectoryPath:@"changeDir"] == NO) {
NSLog(@"\nChange current directory failed");
return ;
}
NSLog(@"\nAfter change current directory.....");
currentPath = [fm currentDirectoryPath];
NSLog(@"\nCurrentpath is : \n%@", currentPath);
}
return ;
}
Foundation 框架 NSFileManager,NSData 简单的文件操作的更多相关文章
- 本地Eclipse连接HDFS进行简单的文件操作
昨天总结了一点自己在搭建Hadoop完全分布式环境过程中遇到的几个小问题以及解决方案,今天在搭建成功的环境中进行了简单的文件操作,包括:文件目录的创建.文件的创建.本地文件的上传.文件的重命名.文件的 ...
- python反转字符串(简单方法)及简单的文件操作示例
Python反转字符串的最简单方法是用切片: >>> a=' >>> print a[::-1] 654321 切片介绍:切片操作符中的第一个数(冒号之前)表示切片 ...
- scala简单的文件操作
1.scala写入文件操作 package com.test import java.io.File import java.io.PrintWriter /** * scala文件写入 */ obj ...
- python作业完成简单的文件操作
题目 请创建以学号命名的目录,在该目录中创建名称为file1.txt的文件,并将自己的个人信息(序号.姓名以及班级)等写入该文件:然后并读取文件中的内容到屏幕上:接着重新命名该文件为file2.txt ...
- Objective-C NSFileManager的使用 各种文件操作
所有方法 都很简单,大概记录一下,写文件并没有是追加的方式而是简单的覆盖 //创建文件夹 - (BOOL)creatDir:(NSString*)newDirName at:(NSString*)di ...
- php简单的文件操作
(1)先要想好要操作哪个文件? (2)确定文件的路径? (3)要有什么文件管理功能? 一.先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击"返 ...
- python实现简单的登陆认证(含简单的文件操作)
需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(本 ...
- C#中简单的文件操作实例
using System; using System.IO; namespace Demo { class Program { static string tmpPath = @"D:/Lg ...
- Golang简单写文件操作的四种方法
package main import ( "bufio" //缓存IO "fmt" "io" "io/ioutil" ...
随机推荐
- [方法] ubuntu12.04开启root账户
ubuntu 12.04使用LightDM显示管理器,默认禁止root账户登录. 通过修改/etc/lightdm/lightdm.con文件可以打开root登录权限. 方法很简单,只要在lightd ...
- SelectDirectory使用方法以及EnableTaskWindows
SelectDirectory使用方法 格式 Delphi syntax: On Windows: function SelectDirectory(const Caption: string; co ...
- restful_api
http://www.ruanyifeng.com/blog/2014/05/restful_api.html
- ADB几种常见的错误及解决方法
下面列举出几种常见的错误及解决方法. Q1:无效的安装包,安装包已损坏[INSTALL_FAILED_INVALID_APK] A1:请检查安装包是否完整.如果是xpk包,可以通过 手动安装xpk来检 ...
- nova的wsgi介绍【WIP】
有关openstack的所有的帖子. https://www.ustack.com/blog/openstack_hacker/#Nova_Workflow 网上已经很多的分析文章了: http:// ...
- [笔记]线性回归&梯度下降
一.总述 线性回归算法属于监督学习的一种,主要用于模型为连续函数的数值预测. 过程总得来说就是初步建模后,通过训练集合确定模型参数,得到最终预测函数,此时输入自变量即可得到预测值. 二.基本过程 1. ...
- 剑指offer 25 二叉树中和为某一值的路径
非递归方法: class Solution { public: vector<vector<int>> FindPath(TreeNode* root,int expectNu ...
- android样式跟主题
简单说类似与自定义控件,只不过自定义控件针对的是view 而样式与主题针对的是属性.元素 在TexvView中引入样式 layout.xml <?xml version="1.0&qu ...
- curl返回值写入内存的场景
直接上代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cur ...
- 将图片文件以byte的形式从导数据库中
byte[] FileByteArray = new byte[FileLength]; //图象文件临时储存Byte数组 //Stream StreamObject ...