OC语言 系统文件
来源:互联网 发布:js string startswith 编辑:程序博客网 时间:2024/06/05 19:30
系统文件
//
// ViewController.m
// Tank-OS12-01
//
// Created by ibokan on 15/12/25.
// Copyright © 2015年谭其伟. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
NSString *string = [NSStringstringWithContentsOfURL:[NSURLURLWithString:@"http://www.baidu.com"]encoding:NSUTF8StringEncodingerror:nil];
// NSLog(@"string = %@",string);
/*
ios文件系统
当第一次启动app时,ios操作系统就为此app创建了一个文件系统,该文件系统下默认有四个目录,分别是:
Documents:存储用户在操作app时产生的数据,此目录下的数据可以通过iCloud进行同步.
Library:用户偏好设置数据,通常和此类NSUserDefaults搭配使用,此目录下的数据可以通过iCloud进行同步.
tmp:存放临时数据的(缓存).此目录下的数据不会通过iCloud同步.
app包:开发者不会操作此目录,通常是通过此类NSBundle类来获取包内资源,如工程素材.
*/
//获取程序根目录
NSString *rootPath = NSHomeDirectory();
//拼接
NSString *documentsPath = [rootPath stringByAppendingFormat:@"/Documents"];
//或者
documentsPath = [rootPath stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",documentsPath);
// NSLog(@"rootPath = %@",rootPath); //NSLog增强版,可以log出多一行
// //条件断点
//
// for (int i = 0; i <100; i++)
// {
// //断点打在这
// }
//
//常用的获取Documents目录的方法
/*
<#NSSearchPathDirectory directory#>:指定了搜索的路径名称
<#NSSearchPathDomainMask domainMask#>:限定了文件的检索范围只在沙盒(sandbox)内部
<#BOOL expandTilde#>:决定了是否展开波浪线符号,展开后才是完整的路径,这个布尔值一直为YES时才会展开完整的路径.
该方法返回的为一个数组,在iphone中由于只有一个唯一路径,所以直接取数组第一个元素即可.
*/
documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex:0];
NSLog(@"%@",documentsPath);
//下载一个视频文件到Documents目录下的Video文件夹
//这里我们封装一个函数,使得这个函数返回的是我们在Documents目录下想要的文件夹的路径.
/*
NSString *videoPath = [self creatDirInDocuments:@"Video"];
NSLog(@"videoPath = %@",videoPath);
if (videoPath != nil) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videoUrlString = @"http://v8.tv.cctv5.cctv.com/r5wbth/4d/e7/4de76971-63f4-4717-f28b-03d757a7704f/mp4h.mp4";
//取最后一个/后面的字符串,然后再与videoPath拼接,会自动添加'/';
NSString *fileVideo = [videoPath stringByAppendingPathComponent:[videoUrlString lastPathComponent]]; //最后面的[里面是取最后一个'/'后的后缀]
//如果文件夹不存在该路径
if (![fileManager fileExistsAtPath:fileVideo]) {
//转码
// videoUrlString = [videoUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
[videoUrlString stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
//我们的图片/视频/音频等在网络中都是以二进制文件传输,所以这里我们拿到的是data
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrlString]];
if (data == nil) {
NSLog(@"网络不给力,稍后再试");
}
else
{
//用单例类NSFileManager的对象,将文件写入本地
BOOL isSuccess = [fileManager createFileAtPath:fileVideo contents:data attributes:nil];
if (isSuccess) {
NSLog(@"视频下载成功");
}
else
{
NSLog(@"视频下载失败");
}
}
}
}
*/
//字符串拼接,获取Library目录
NSString *libraryPath = [rootPath stringByAppendingString:@"/Library"];
NSLog(@"libraryPath = %@",libraryPath);
//第二种
libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)objectAtIndex:0];
NSLog(@"libraryPath = %@",libraryPath);
//tmp
NSString *tmpString = NSTemporaryDirectory();
NSLog(@"tmpString = %@",tmpString);
NSArray *imgsArray =@[@"http://img0.imgtn.bdimg.com/it/u=4149130001,3742834577&fm=11&gp=0.jpg",@"http://img4.imgtn.bdimg.com/it/u=3559701691,1695542011&fm=11&gp=0.jpg",@"http://img1.imgtn.bdimg.com/it/u=3021209900,2363537985&fm=11&gp=0.jpg",@"http://b88.photo.store.qq.com/psb?/V10NeaS93Z1jjj/ea6jOdEb3b.pN.JFn*cdKnHrDXWjCJdETiNdNpxw1VY!/b/dFgAAAAAAAAA&bo=AALSAwAAAAAFAPA!&rf=viewer_4"];
//下载一些图片放到tmp目录下的Imgs文件夹下,如果有些图片已经下载了,那么不会继续下载
NSString *imgsTmpPath = [selfcreateDirInTmp:@"Imgs"];
if (imgsTmpPath != nil) {
// NSLog(@"imgsTmpPath = %@",imgsTmpPath);
for (int i =0; i <imgsArray.count; i++) {
NSString *imgsString = [imgsArray[i]lastPathComponent];
//获得每张图片的路径
NSString *imgsPath = [imgsTmpPath stringByAppendingPathComponent:imgsString];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:imgsPath]) { //一定要加个'!'非
NSString *urlString = [imgsArray[i]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
//将urlString转化成data
NSData *data2 = [NSDatadataWithContentsOfURL:[NSURLURLWithString:urlString]];
if (data2 == nil) {
NSLog(@"网络有问题,请稍后再试");
}
else
{
BOOL isSuccess = [data2 writeToFile:imgsPath atomically:YES];
if (isSuccess) {
NSLog(@"图片下载成功");
}
else
{
NSLog(@"图片下载失败");
}
}
}
}
}
//将字符串写入到本地
NSString *targetString = @"Hello guys";
BOOL flag = [targetStringwriteToFile:[imgsTmpPath stringByAppendingPathComponent:@"a.txt"]atomically:YESencoding:NSUTF8StringEncodingerror:nil];
if (flag) {
NSLog(@"字符串写入本地成功");
}
else
{
NSLog(@"字符串写入本地失败");
}
//将数组写入本地文件
NSArray *array =@[@"刘林强",@"黄智威",@"何鹏",@"许毓"];
flag = [array writeToFile:[imgsTmpPathstringByAppendingPathComponent:@"array.txt"]atomically:YES];
if (flag) {
NSLog(@"数组写入成功");
}
else
{
NSLog(@"数组写入失败");
}
//将字典写入本地
NSDictionary *dictionary =@{@"name":@"Rick",@"age":@25,@"address":@"GZ"};
flag = [dictionary writeToFile:[imgsTmpPathstringByAppendingPathComponent:@"dictionary.txt"]atomically:YES];
if (flag) {
NSLog(@"字典写入本地成功");
}
else
{
NSLog(@"字典写入本地失败");
}
//计算文件大小
//获得将要计算的文件夹
NSFileManager *fileManager = [NSFileManagerdefaultManager];// 获得单例类(可以写到头顶,只写一次就好)
//获得imgs目录下文件名组成的数组
NSArray *imgsFileArray = [fileManager subpathsAtPath:imgsTmpPath];
NSLog(@"imgsFileArray = %@",imgsFileArray);
//提取每个文件大小并累加
CGFloat count = 0; //累加用的
for (NSString *elein imgsFileArray) {
NSData *data = [NSDatadataWithContentsOfFile:[imgsTmpPath stringByAppendingPathComponent:ele]]; //后面的是取得 /路径,前面是转化为data
count += data.length; //取出的长度是字节Byte
}
count = count/1024/1024; //由Byte转化为 MB
NSLog(@"缓存文件的大小为: %.2fM",count);
//删除文件
for (NSString *elein imgsFileArray) {
BOOL isSuccess = [fileManager removeItemAtPath: [imgsTmpPath stringByAppendingPathComponent:ele]error:nil];
if (isSuccess) {
NSLog(@"删除成功");
}
else
{
NSLog(@"删除失败");
}
}
//app包,获取包内图片,显示在UI
NSBundle *bundle = [NSBundlemainBundle];
NSLog(@"bundle = %@",bundle); //获取app包路径
NSString *imgPath = [bundle pathForResource:@"girl" ofType:@".png"]; //.jpg 或者 png(不用点)(其实.png也可以的)
NSData *data = [NSDatadataWithContentsOfFile:imgPath];
NSLog(@"data = %@",data);
self.imagefile.image = [UIImageimageWithData:data];
}
-(NSString *)creatDirInDocuments:(NSString *)dirName
{
//获得Documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex:0];
//拼接成我们想要的文件的路径的字符串...此方法自动加'/';
NSString *dirDocuments = [documentsPath stringByAppendingPathComponent:dirName];
//创建文件夹
//获取NSFileManager单例类,用于文件操作,在哪里声明都一样的
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//判断本地是否存在某个文件或文件夹
BOOL isExist = [fileManager fileExistsAtPath:dirDocuments];
if ( !isExist ) {
//创建文件夹
NSError *error;
BOOL isSuccess = [fileManagercreateDirectoryAtPath:dirDocuments withIntermediateDirectories:YESattributes:nilerror:&error];
if (!isSuccess) {
//如果文件创建失败,将打印错误的信息
NSLog(@"error = %@",error.debugDescription); //查看为什么错
dirDocuments = nil;
}
}
return dirDocuments;
}
-(NSString *)createDirInTmp:(NSString *)dirName
{
NSString *tmp =NSTemporaryDirectory();
//想要文件的路径
NSString *dirPath = [tmp stringByAppendingPathComponent:dirName];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:dirPath]) {
NSError *error;
BOOL isSuccess = [fileManagercreateDirectoryAtPath:dirPath withIntermediateDirectories:YESattributes:nilerror:&error];
if (!isSuccess) {
dirPath = nil;
NSLog(@"error = %@",error.debugDescription);
}
}
return dirPath;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// PrefixHeader.pch
// Tank-OS12-01
//
// Created by ibokan on 15/12/25.
// Copyright © 2015年谭其伟. All rights reserved.
//
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
//A better version of NSLog
#define NSLog(format, ...) do { \
fprintf(stderr, "<%s : %d> %s\n", \
[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \
__LINE__, __func__); \
(NSLog)((format), ##__VA_ARGS__); \
fprintf(stderr, "-------\n"); \
} while (0)
#endif /* PrefixHeader_pch */
<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>用以实现对http协议的访问与读取
- OC语言 系统文件
- oc语言文件管理
- 黑马程序员 - OC语言 - NSArray 读写文件
- OC语言学习11-pilst文件操作
- oc语言
- OC 语言
- Swift基础之Swift调用OC语言文件使用步骤
- 黑马程序员-OC语言-OC多文件开发介绍(听课笔记)
- OC语言--OC语言基础、类
- ios开发-如何将oc文件转成c,c++语言文件,后缀名为cpp
- OC语言学习-其他
- OC语言01
- OC语言特点
- OC语言02
- OC语言03
- OC语言中的字典
- oc基础语言
- OC语言前期准备
- 安装xcache3.0.3/3.2,为php加速
- android开发之路-----------转自任玉刚大神
- 线程池
- Android 编程技巧之 ----- 最佳 Activity 数据传递
- Linux信号实践(1) --Linux信号编程概述
- OC语言 系统文件
- 排序
- 程序员励志名言
- js获取文本显示宽度
- Dom4J解析xml文档
- Linux信号实践(2) --信号分类
- 一个进程最多可以创建线程的数目
- Android学习 之 ColorStateList按钮文字变色
- TableView性能优化——动态cell适应工具(UITableView+FDTemplateLayoutCell)