iOS file operations

Source: Internet
Author: User

Objective-c file and directory operations, iOS file operations, Nsfilemanager using file operations:

Objective-c uses the Nsfilemanager class to manage and manipulate files, directories, Nsfilemanager, files, or directories that are unique to the path name of the file used. Each pathname is a NSString object.

Nsfilemanager objects create instances by using the Defaultmanager method
Gca

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

Delete a file
[FM removeitematpath:@ "filename" error:null];

error: The parameter is a pointer to the nserror object that can provide the wrong information. If NULL is specified, the default behavior is used, and the return value is a method of type bool, and the operation successfully returns YES instead of returning no

Determine if a file has been deleted
if ([FM removeitematpath:@ "filename" error:null]==no) {
NSLog (@ "File deletion failed");
return 1;
}

Nsfilemanager commonly used file methods:

-(nsdata*) Contentsatpath:path reading data from a file

-(BOLL) Createfileatpath:path contents: (nsdata*) data attributes:attr write to a file

-(BOOL) Removeitematpath:path error:err Delete a file

-(BOOL) moveitematpath:from topath:to error:err Rename or move a file (to cannot be existing)

-(BOOL) copyitematpath:from topath:to error:err Copy file (to cannot be existing)

-(BOOL) contentsequalatpath:path1 andpath:path2 Compare the contents of these two files

-(BOOL) fileexistsatpath:path test file exists

-(BOOL) Isreadablefileatpath:path test file exists and can perform read operations

-(BOOL) Iswritablefileatpath:path test whether the file exists and whether it can perform a write operation

-(nsdictionary*) Attributesofitematpath:path Error:err Get the properties of the file

The property dictionary allows you to specify the permissions of the file to be created, and if the parameter is specified as nil, the file is set to the default permissions.

1, through a program to operate the file:

[CPP]View Plaincopyprint?
  1. //   
  2. //MAIN.M   
  3. //Nsfilemanager_01   
  4. //   
  5. //Created by Swinglife on 13-11-10.   
  6. //Copyright (c) 2013 Swinglife. All rights reserved.   
  7. //   
  8. #import <Foundation/Foundation.h>   
  9. int Main (int argc, const char * argv[])
  10. {
  11. @autoreleasepool {
  12. //File name   
  13. NSString *filename = @"testfile";
  14. NSString *filecontent = @"This is the file content!!!!"  ;
  15. NSData *filedata = [Filecontent datausingencoding:nsutf8stringencoding];
  16. //Create Nsfilemanager instances   
  17. Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
  18. //Create file   
  19. [FM createfileatpath:filename Contents:filedata Attributes:nil];
  20. //Determine if the file exists or does not exist on the end program   
  21. if ([FM fileexistsatpath:filename]==no) {
  22. NSLog (@"file does not exist");
  23. return  1;
  24. }
  25. //Copy files   
  26. if ([FM copyitematpath:filename topath:@"NewFile" error:null]==no) {
  27. NSLog (@"copy failed");
  28. return  2;
  29. }
  30. //test whether two files are the same   
  31. if ([FM contentsequalatpath:filename andpath:@"NewFile"]==no) {
  32. NSLog (@"file is not the same");
  33. return  3;
  34. }
  35. //Rename NewFile   
  36. [FM moveitematpath:@ "newFile " topath:@"NewFile2" Error:null];
  37. //Get the size of the NewFile2   
  38. Nsdictionary *FILEATTR = [fm attributesofitematpath:@"NewFile2" Error:null];
  39. if (fileattr!=nil) {
  40. NSLog (@"File Size:%llu bytes", [[FileAttr Objectforkey:nsfilesize] unsignedlonglongvalue]);
  41. }
  42. //delete files   
  43. [FM removeitematpath:filename Error:null];
  44. //display content of NewFile2   
  45. NSString *data = [NSString stringwithcontentsoffile:@"NewFile2" encoding:nsutf8stringencoding Error:  NULL];
  46. NSLog (@"%@", data);
  47. }
  48. return  0;
  49. }

Nsfilemanager Common directory Methods


-(nsstring*) Currentdirectorypath get current directory

-(BOOL) Changecurrentdirectorypath:path change the current directory

-(BOOL) copyitematpath:from topath:to error:err Copy directory structure

-(BOOL) Createdirectoryatpath:path withintermediatedirectories: (BOOL) flag attributes:attr Create a new directory

-(BOOL) Fileexistsatpath:path isdirectory: (bool*) flag test file is not a directory (store results in flag)

-(nsarray*) contentsofdirectoryatpath:path Error:err Listing directory contents

-(nsdirectoryenumerator*) Enumeratoratpath:path The contents of the enumeration directory

-(BOOL) Removeitematpath:path error:err Delete Empty directory

-(BOOL) moveitematpath:from topath:to error:err Rename or move a directory

2, through a program to operate the directory:

[CPP]View Plaincopyprint?
    1. //   
    2. //MAIN.M   
    3. //Nsfilemanager_02   
    4. //   
    5. //Created by Swinglife on 13-11-10.   
    6. //Copyright (c) 2013 Swinglife. All rights reserved.   
    7. //   
    8. #import <Foundation/Foundation.h>   
    9. int Main (int argc, const char * argv[])
    10. {
    11. @autoreleasepool {
    12. //File directory   
    13. NSString *dirname = @"TestDir";
    14. //Create Nsfilemanager instances   
    15. Nsfilemanager *FM = [Nsfilemanager Defaultmanager];
    16. //Get current directory   
    17. NSString *PATH = [FM Currentdirectorypath];
    18. NSLog (@"path:%@", Path);
    19. //Create a new directory   
    20. [FM createdirectoryatpath:dirname Withintermediatedirectories:yes Attributes:nil Error:null];
    21. //Rename a new directory   
    22. [FM moveitematpath:dirname topath:@"Newdir" Error:null];
    23. //Change the current directory to a new directory   
    24. [FM changecurrentdirectorypath:@"Newdir"];
    25. //Get current working directory   
    26. PATH = [FM Currentdirectorypath];
    27. NSLog (@"path:%@", Path);
    28. }
    29. return  0;
    30. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.