Object-C  文件类的各种操作…

本文来讲述 文件类的各种操作方法,示例如下:

view plaincopy to clipboardprint?
NSString  *fName=@"testfile.m";  
NSFileManager *fm=NULL;  
NSDictionary  *dict=NULL;  
 
 
fm=[NSFileManager defaultManager];  
 
if ([fm fileExistsAtPath:fName]==NO) {  
    NSLog(@"file not exist!");  
      
    return 1;  
 
 
   if ([fm copyPath:fName toPath:@"newfile" handler:nil]==NO) {  
    NSLog(@"copy failed!");  
 
 
if ([fm movePath:@"newfile" toPath:@"newfolder" handler:nil]==NO) {  
    NSLog(@"move failed");  
 
 
if ((dict=[fm fileAttributesAtPath:@"newfolder" traverseLink:NO])==nil) {  
    NSLog(@"get file attributes failed");  
 
 
NSLog(@"file size is %i bytes",[[dict objectForKey:NSFileSize] intValue]);  
 
 
if ([fm removeFileAtPath:@"newfolder" handler:nil]==YES) {  
    NSLog(@"remove successful!");  
 
 NSString  *fName=@"testfile.m";
 NSFileManager *fm=NULL;
 NSDictionary  *dict=NULL;
 
 
 fm=[NSFileManager defaultManager];
 
 if ([fm fileExistsAtPath:fName]==NO) {
  NSLog(@"file not exist!");
  
  return 1;
 }
 
    if ([fm copyPath:fName toPath:@"newfile" handler:nil]==NO) {
  NSLog(@"copy failed!");
 }
 
 if ([fm movePath:@"newfile" toPath:@"newfolder" handler:nil]==NO) {
  NSLog(@"move failed");
 }
 
 if ((dict=[fm fileAttributesAtPath:@"newfolder" traverseLink:NO])==nil) {
  NSLog(@"get file attributes failed");
 }
 
 NSLog(@"file size is %i bytes",[[dict objectForKey:NSFileSize] intValue]);
 
 
 if ([fm removeFileAtPath:@"newfolder" handler:nil]==YES) {
  NSLog(@"remove successful!");
 }
 

NSData类的使用方法

view plaincopy to clipboardprint?
NSData  *fileData=NULL;  
 
 
fm=[NSFileManager defaultManager];  
 
fileData=[fm contentsAtPath:@"testfile.m"];  
 
if (fileData==nil) {  
    NSLog(@"read file failed!");  
 
 
if ([fm createFileAtPath:@"file2" contents:fileData attributes:nil]==YES) {  
    NSLog(@"create file success!");  
 
 NSData  *fileData=NULL;
 
 
 fm=[NSFileManager defaultManager];
 
 fileData=[fm contentsAtPath:@"testfile.m"];
 
 if (fileData==nil) {
  NSLog(@"read file failed!");
 }
 
 if ([fm createFileAtPath:@"file2" contents:fileData attributes:nil]==YES) {
  NSLog(@"create file success!");
 }
 

对路径的操作方法:

view plaincopy to clipboardprint?
NSString  *fName=@"testDir";  
NSFileManager *fm=NULL;  
//NSDictionary  *dict=NULL;  
NSString  *path=NULL;  
 
 
fm=[NSFileManager defaultManager];  
 
path=[fm currentDirectoryPath];  
 
NSLog(@"currentDir is: %@",path);  
 
//create a new directory  
[fm createDirectoryAtPath:fName attributes:nil];  
 
//rename the directory  
[fm movePath:fName toPath:@"newDir" handler:nil];    
 
//change currentdirectory  
[fm changeCurrentDirectoryPath:@"newDir"];  
 
path=[fm currentDirectoryPath];  
 
NSLog(@"currentDir is: %@",path); 
 NSString  *fName=@"testDir";
 NSFileManager *fm=NULL;
 //NSDictionary  *dict=NULL;
 NSString  *path=NULL;
 
 
 fm=[NSFileManager defaultManager];
 
 path=[fm currentDirectoryPath];
 
 NSLog(@"currentDir is: %@",path);
 
 //create a new directory
 [fm createDirectoryAtPath:fName attributes:nil];
 
 //rename the directory
 [fm movePath:fName toPath:@"newDir" handler:nil]; 
 
 //change currentdirectory
 [fm changeCurrentDirectoryPath:@"newDir"];
 
 path=[fm currentDirectoryPath];
 
 NSLog(@"currentDir is: %@",path);

枚举某个目录的所有内容:

view plaincopy to clipboardprint?
NSFileManager  *fm=NULL;  
NSDirectoryEnumerator *dirEnum=NULL;  
NSArray    *dirArray=NULL;  
NSString   *path=NULL;  
 
 
fm=[NSFileManager  defaultManager];  
 
path=[fm currentDirectoryPath];  
 
dirEnum=[fm enumeratorAtPath:path];  
 
while ((path=[dirEnum nextObject])!=nil) {  
    dirArray=[fm directoryContentsAtPath:[fm currentDirectoryPath]];  
 
 
NSLog(@"%@",dirArray); 
 NSFileManager  *fm=NULL;
 NSDirectoryEnumerator *dirEnum=NULL;
 NSArray    *dirArray=NULL;
 NSString   *path=NULL;
 
 
 fm=[NSFileManager  defaultManager];
 
 path=[fm currentDirectoryPath];
 
 dirEnum=[fm enumeratorAtPath:path];
 
 while ((path=[dirEnum nextObject])!=nil) {
  dirArray=[fm directoryContentsAtPath:[fm currentDirectoryPath]];
 }
 
 NSLog(@"%@",dirArray);

NSFileHandle的使用示例:

view plaincopy to clipboardprint?
NSFileHandle  *inFile, *outFile;  
NSData   *buffer;  
 
 
inFile=[NSFileHandle fileHandleForReadingAtPath:@"testfile.m"];  
 
[[NSFileManager defaultManager]createFileAtPath:@"newfile1" contents:nil attributes:nil];  
 
outFile=[NSFileHandle fileHandleForWritingAtPath:@"newfile1"];  
 
[outFile truncateFileAtOffset:0];  
 
buffer=[inFile readDataToEndOfFile];  
 
[outFile writeData:buffer];  
 
[inFile closeFile];  
[outFile closeFile]; 

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/newjerryj/archive/2011/03/20/6263134.aspx

ssm管理系统文件中util文件里PageHelper类解释以下代码package com.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; public class PageHelper { public static void getPage(List<?> list, String name, List<String> nameList, List<String> valueList, int pageSize, String number, HttpServletRequest request, String method) { StringBuffer buffer = new StringBuffer(); String name2 = name.substring(0, 1).toUpperCase() + name.substring(1); String path = ""; String action = "getAll" + name2 + ".action"; if (method != null) { action = "query" + name2 + "ByCond.action"; } List<Object> objList = new ArrayList<Object>(); if (nameList != null && valueList != null) { for (int i = 0; i < nameList.size(); i++) { path += "&" + nameList.get(i) + "=" + valueList.get(i); } } int pageNumber = list.size(); int maxPage = pageNumber; if (maxPage % pageSize == 0) { maxPage = maxPage / pageSize; } else { maxPage = maxPage / pageSize + 1; } if (number == null) { number = "0"; } int start = Integer.parseInt(number) * pageSize; int over = (Integer.parseInt(number) + 1) * pageSize; int count = pageNumber - over; if (count <= 0) { over = pageNumber; } for (int i = start; i < over; i++) { Object obj = list.get(i); objList.add(obj); } buffer.append("&nbsp;&nbsp;共为"); buffer.append(maxPage); buffer.append("页&nbsp; 共有"); buffer.append(pageNumber); buffer.append("条&nbsp; 当前为第"); buffer.append((Integer.parseInt(number) + 1)); buffer.append("页 &nbsp;"); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("首页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=0" + path + "\">首页</a>"); } buffer.append("&nbsp;&nbsp;"); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("上一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) - 1) + "" + path + "\">上一页</a>"); } buffer.append("&nbsp;&nbsp;"); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("下一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) + 1) + "" + path + "\">下一页</a>"); } buffer.append("&nbsp;&nbsp;"); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("尾页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (maxPage - 1) + "" + path + "\">尾页</a>"); } String html = buffer.toString(); request.setAttribute("html", html); request.setAttribute(name + "List", objList); }
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值