Foundation framework NSFileManager, NSData simple file operations

A simple showcase of use NSFileManager

#import <Foundation / Foundation.h> int main ( int argc, const char * the argv []) 
{ 
    @autoreleasepool { // create a file management target 
        the NSFileManager FM * = [the NSFileManager defaultManager];
         // file name to be operated 
        NSString * = fname @ " myfile " ;
         // get the dictionary file 
        NSDictionary * attr;
         // current path 
        NSString * path;
         // get the current path 
        path = [fm currentDirectoryPath];
         //

 

        NSLog (@ "\ nThe path Current IS:% @", path); 
        
        // detecting whether the file exists 
        IF ([FM fileExistsAtPath: fname] == NO) {
             // if it does not exist a file 
            [fm createFileAtPath: fname Contents: NULL the Attributes: nil];
             // NSLog (@ "! \ nThe iS file not exist");
             // return 0; 
        }
         // copy to create a new file, the new file If there is already an error 
        IF ([fm copyItemAtPath : toPath fname: @ " newFile " error: NULL] == NO) { 
            NSLog ( @ " \ n-The Can Not Copy File " );
             return  . 1 ; 
        }
        // detect whether the file content two identical 
        IF ([FM contentsEqualAtPath: andPath fname: @ " newFile " ] == NO) { 
            NSLog ( @ " \ nThe Contents IS Not Same " );
             return  2 ; 
        } 
        // move or rename file 
        IF ([FM moveItemAtPath: @ " newFile "   toPath: @ " myfile2 " error: NULL] == NO) { 
            NSLog ( @ " \ nCan't Change The name " );
             return  . 3  ;
        } 
        // Get the data dictionary file 
        IF ((attr = [FM attributesOfItemAtPath: error fname: NULL]) == nil) { 
            NSLog ( @ " \ nget attributets failed " );
             return  . 4 ; 
        } 
        // File Size 
        NSLog ( @ " @% " , attr [NSFileSize]);
         // file type 
        NSLog ( @" % @ " , attr [NSFileType]);
         // creator 
        NSLog ( @" % @ " , attr [NSFileOwnerAccountName]);
         //
         NSLog ( @ " % @ ", attr[NSFileCreationDate]);
        //显示文件内容
        NSLog(@"\n Show the file contents");
        NSLog(@"\n%@", [NSString stringWithContentsOfFile: fname encoding:NSUTF8StringEncoding error:NULL]);
    }
    return 0;
}

 Second, by making a copy of the complete NSData

. 1  int main ( int argc, const  char * the argv [])
 2  {
 . 3  
. 4      @autoreleasepool {
 . 5          // accomplished by making copies of documents NSDate 
. 6          the NSFileManager FM * = [the NSFileManager defaultManager];
 . 7          NSData * dt;
 . 8          
. 9          dt = [FM contentsAtPath: @ " myfile " ];
 10          
. 11          IF (dt == nil) {
 12 is              NSLog ( @ " the Read File failed .... " );
 13 is             return  0 ;
 14          }
 15          
16          // copy the contents of the buffer to a file in NSData 
. 17          IF ([FM createFileAtPath: @ " myFavoriteFile " Contents: dt Attributes: nil] == NO) {
 18 is              NSLog ( @ " Creat Backups failed " );
 . 19              return  . 1 ;
 20 is          }
 21 is          
22 is          // reads out the contents of the file 
23 is          NSLog ( @" \ @ n-% " , [NSString stringWithContentsOfFile: @" myFavoriteFile " encoding: NSUTF8StringEncoding error:NULL]);
24     }
25     return 0;
26 }

Third, the simple directory operations

 1 #import <Foundation/Foundation.h>
 2 
 3 int main(int argc, const char * argv[])
 4 {
 5 
 6     @autoreleasepool {
 7         NSString *newDir = @"newDir";
 8         NSString *currentPath;
 9         NSFileManager *fm = [NSFileManager defaultManager];
10         
11         //获取当前路径
12         currentPath = [fm currentDirectoryPath];
13         NSLog(@"\ nCurrentpath IS: \ @ n-% " , currentPath);
 14          
15          // in the current directory create a new directory 
16          IF ([FM createDirectoryAtPath: newDir withIntermediateDirectories: TRUE Attributes: nil error: NULL] == NO) {
 . 17              NSLog ( @ " \ nCouldn't creat The Directory ... " );
 18 is              return  0 ;
 . 19          }
 20 is          
21 is          // change the path name 
22 is          IF ([FM moveItemAtPath: newDir toPath: @ " changeDir " error: NULL] == NO) {
 23              NSLog ( @ "\nChange directory name failed");
24             return 2;
25         }
26         
27         //更改当前路径
28         if ([fm changeCurrentDirectoryPath:@"changeDir"] == NO) {
29             NSLog(@"\nChange current directory failed");
30             return 1;
31         }
32         NSLog(@"\nAfter change current directory.....");
33         currentPath = [fm currentDirectoryPath];
34         NSLog(@"\nCurrentpath is : \n%@", currentPath);
35     }
36     return 0;
37 }

 

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/3957603.html

Guess you like

Origin blog.csdn.net/weixin_34254823/article/details/93438720