适合有java和C基础同学迅速了解OC
- #import <Foundation/Foundation.h>
- #include <stdio.h>
- #include "stdlib.h"
- /*
- 1>找出文件夹下所有的C和OC代码文件及TXT文件,
- 计算出这些文件中总共的行数,
- 2>将这些文件复制到另一个文件夹
-
- */
- // 创建文件夹
- int cmkdir(char *path,int mode);
- //计算 文件或文件夹内文本总行数
- int fileLineCount(NSString * path);
- //判断是否文本文件
- int isTextFile(NSString *path);
- //计算文本文件行数
- int textLineCount(NSString * path);
- //将路径下所有文本文件拷贝到新文件夹
- int copyTextFileToDir(NSString *path,NSString *toDir);
- //拷贝文本文件
- int copyTextFileToFile(NSString *srcPath,NSString *destPath);
- int main()
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
- NSString * path=@"C:/Users/Administrator/Desktop";
- char * toDir="C:/Users/Administrator/Desktop/codeDir";
- //计算文件夹下文本文件内容总行数
- int count = fileLineCount(path);
- NSLog(@"total lines =%d",count);
-
- // 将文件夹下文本文件复制到某文件夹
- cmkdir(toDir,0777);
- int fileCount=copyTextFileToDir(path,
- [NSString stringWithUTF8String:toDir]);
- NSLog(@"共复制了文本文件个数 =%d",fileCount);
-
- [pool drain];
- return 0;
-
- }
- int cmkdir(char *path,int mode)
- {
- int flag=0;
- //access判断文件是否有给定的存取方式,00表示判断文件是否存在
- //返回0表示 符合给定方式,否则返回-1
- flag=access(path,00);
- //存在直接返回
- if(!flag)
- return flag;
-
- //mkdir(path,mode) 0777为八进制,最大访问权限
- //返回0表示成功,-1表示出错。
- flag = mkdir(path,mode);
- if(flag)
- {
- printf("创建文件夹失败");
- getch();
- exit(1);
- }
- return flag;
- }
- int copyTextFileToDir(NSString *path,NSString *toDir)
- {
- int count=0;
- BOOL isDir,exists;
- NSFileManager *fm=[NSFileManager defaultManager];
- exists = [fm fileExistsAtPath:path isDirectory:&isDir];
- if(!exists) return count;
- if(isDir)
- {
- NSArray *files=[fm contentsOfDirectoryAtPath:path error:nil];
- int idx ;
- for(idx=0;idx<[files count];idx++)
- count+=copyTextFileToDir([NSString stringWithFormat:
- @"%@/%@",path,[files objectAtIndex:idx]],toDir);
-
- }else
- {
- if(isTextFile(path))
- {
- // 是文本文件,则按照原来文件名称复制到新文件夹
- NSArray *ary = [path componentsSeparatedByString:@"/"];
- count=copyTextFileToFile(path,[NSString stringWithFormat:
- @"%@/%@",toDir,[ary objectAtIndex:[ary count]-1]]);
- }
- }
- return count;
- }
- int copyTextFileToFile(NSString *srcPath,NSString *destPath)
- {
- NSString *content = [NSString stringWithContentsOfFile:srcPath];
- return [content writeToFile:destPath atomically:YES];
- }
- int fileLineCount(NSString *path)
- {
- int count=0;
- BOOL isDir,exists;
- NSFileManager *fm=[NSFileManager defaultManager];
- exists=[fm fileExistsAtPath:path isDirectory:&isDir];
-
- if(exists&&isDir)
- {
- // 返回文件名的OC数组
- NSArray *files=[fm contentsOfDirectoryAtPath:path error:nil];
- int idx;
- for(idx=0;idx<[files count];idx++)
- // 返回的仅仅是文件名,需要跟路径拼接成全路径名
- count+=fileLineCount([NSString stringWithFormat:@"%@/%@",
- path,[files objectAtIndex:idx]]);
-
- }else if(exists)
- {
- if(isTextFile(path))
- {
- count=textLineCount(path);
- //将查找到的字符串替换成空字符串
- NSString *shortPath=[path stringByReplacingOccurrencesOfString:
- @"C:/Users/Administrator/" withString:@""];
- NSLog(@"%@---%d",shortPath,count);
- }
- }
- return count;
-
-
- }
- int isTextFile(NSString *path)
- {
- NSString *extension=[[path pathExtension] lowercaseString];
- NSArray *extensionAry=[NSArray arrayWithObjects:
- @"m",@"c",@"cpp",@"txt",@"h",nil];
- //拓展名是其中任何一个就是文本文件
- BOOL flag=0;//默认不是
- int idx;
- for(idx=0;idx<[extensionAry count]&&!flag;idx++)
- {
- flag=[extension isEqualToString:[extensionAry objectAtIndex:idx]];
- }
- return flag;
-
- }
- int textLineCount(NSString * path)
- {
- NSString *content=[NSString stringWithContentsOfFile:path];
- //以\n来分割字符串
- NSArray * ary=[content componentsSeparatedByString:@"\n"];
- /*
- int i;
- for(i=0;i<[ary count];i++)
- NSLog(@"%d--%@",i,[ary objectAtIndex:i]);
- */
- return [ary count];
- }
复制代码
NSFileManager:
提供了文件操作的方法,本身是单例模式;
//获取方法
+(id)defaultManager;
//返回file是否存在 和 file是否是dir
-(BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDir;
//返回path下所有文件及文件夹的名称,以NSArray保存这些NSString
-(NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing*)error;
NSString 中常用的文件操作相关方法:
//取得文件扩展名,即最后一个点之后的字符串(不包括点)
-(NSString *)pathExtension;
//字符串比较
-(BOOL)isEqualToString:(NSString *)otherStr;
//英文部分转小写
-(NSString *)lowercaseString;
//转大写
-(NSString *)uppercseString;
//将range内的字符替换成新字符
-(NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)rep;
//将字符串出现的 串替换成新串
-(NSString *)stringByReplacingOccurrencesOfString:(NSString *)oStr withString:(NSString *)newStr;
//split
-(NSArray *)componentsSeparatedByString:(NSString *)separator;
|
|