iOS 檔案學習筆記,ios檔案學習筆記

來源:互聯網
上載者:User

iOS 檔案學習筆記,ios檔案學習筆記

檔案操作

foundation 架構中利用檔案系統對檔案進行操作。

基本操作是由NSFileManager類提供的,主要功能如下:

  • 建立一個新檔案
  • 檔案中讀取資料
  • 寫入資料
  • 檔案的重新命名
  • 刪除檔案
  • 測試檔案是否存在
  • 確定檔案的大小以及其他屬性
  • 複製檔案
  • 測試兩個檔案是否相同
用NSFileHandle類,可以開啟檔案並對其進行讀寫操作。
  • 開啟檔案,執行讀寫操作
  • 在檔案中尋找指定位置
  • 從檔案中讀取指定書目的位元組,或寫入指定數目的位元組資料

- (NSData *)contentsAtPath:(NSString *)path;

從一個檔案中讀取資料

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;


向檔案中寫入資料

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

刪除檔案

- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

移動檔案

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

複製檔案

- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;


比較兩個檔案內容是否相同

- (BOOL)fileExistsAtPath:(NSString *)path;

檔案是否存在

- (BOOL)isReadableFileAtPath:(NSString *)path;


檔案是否能夠執行讀操作

- (BOOL)isWritableFileAtPath:(NSString *)path;


檔案是否能執行寫操作

- (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error

擷取檔案的屬性

- (BOOL)setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error

設定檔案屬性



每個路徑都是NSString對象。~用作使用者主目錄的縮寫。. 表示目前的目錄..表示父目錄NSFileManager 類的建立
<span style="font-family:Comic Sans MS;font-size:14px;">NSFileManager *fm = [NSFileManager defaultManager];</span>

NSData類操作檔案時需要頻繁的將資料讀入一個緩衝區,NSData類來建立設定緩衝區。
常見的NSFileManager目錄方法

@property (readonly,copy) NSString *currentDirectoryPath;

擷取目前的目錄

- (BOOL)changeCurrentDirectoryPath:(NSString *)path;

更改目前的目錄

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

複製檔案

- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error

建立一個目錄並設定屬性

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;

測試路徑是否問目錄

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

列出目錄內容

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error


刪除目錄

- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error


移動或重新命名目錄

- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path;


枚舉目錄內容,如果還是目錄會繼續深入



使用路徑:NSPathUtilities.hNSPathUtilities.h包含了NSString的函數的分類擴充,它能夠操作路徑名。應該儘可能的使用這些函數。
常用的路徑工具方法

+ (NSString *)pathWithComponents:(NSArray *)components;

根據組件中的元素構造路徑

@property (readonly,copy) NSArray *pathComponents;

析構路徑,獲得組成此路徑的各個部分

@property (readonly,copy) NSString *lastPathComponent;

擷取路徑的最後一個組成部分

@property (readonly,copy) NSString *pathExtension;

從組件最後部分擷取副檔名

- (NSString *)stringByAppendingPathComponent:(NSString *)str;

將str添加到現有路徑的末尾

- (NSString *)stringByAppendingPathExtension:(NSString *)str;

將副檔名添加到末尾

@property (readonly,copy) NSString *stringByDeletingLastPathComponent;

刪除最後組成部分

@property (readonly,copy) NSString *stringByDeletingPathExtension;

刪除最後部分的副檔名

@property (readonly,copy) NSString *stringByExpandingTildeInPath;

將字串中的帶字元擴充為主目錄(~)

@property (readonly,copy) NSString *stringByResolvingSymlinksInPath;

解析路徑中的符號連結

@property (readonly,copy) NSString *stringByStandardizingPath;

通過嘗試解~ .  .. 和符號連結來標準化路徑


使用NSProcessInfo類NSProcessInfo類中包含一些方法,允許你設定或檢索正在啟動並執行應用程式(即進程)的各種類型的資訊NSProcessInfo類方法

+ (NSProcessInfo *)processInfo;

返回當前線程的資訊

@property (readonly,copy) NSArray *arguments;

以string對象數位形式返回當前進程的參數

@property (readonly,copy) NSDictionary *environment;

返回字典,描述當前環境變數

@property (readonly)int processIdentifier;

返回進程表示符,它是作業系統賦予進程的唯一數字

@property (readonly,copy) NSString *globallyUniqueString;

每次調用這個方法時,返回不同的單值字串。可用這個字串產生單值臨時檔案名稱

@property (readonly,copy) NSString *hostName;

返回主機系統的名稱

- (NSUInteger)operatingSystem

返回表示作業系統的數字

- (NSString *)operatingSystemName 

返回作業系統的名稱

@property (readonly,copy) NSString *operatingSystemVersionString;

返回作業系統的目前的版本

@property (copy)NSString *processName;

將當前進程名稱設定為name




基本的檔案操作NSFileHandle使用NSFileHandle類提供的方法,允許更有效地使用檔案。處理檔案時經曆三個步驟: 常用的NSFileHandle方法

+ (instancetype)fileHandleForReadingAtPath:(NSString *)path;

開啟一個檔案準備讀取

+ (instancetype)fileHandleForWritingAtPath:(NSString *)path;

開啟一個檔案準備寫入

+ (instancetype)fileHandleForUpdatingAtPath:(NSString *)path;

開啟一個檔案準備更新(讀寫)

@property (readonly,copy) NSData *availableData;

從裝置或者通道返回可用的資料

- (NSData *)readDataToEndOfFile;

讀取其餘的資料直到檔案末尾

- (NSData *)readDataOfLength:(NSUInteger)length;

從檔案中讀取指定bytes的內容

- (void)writeData:(NSData *)data;

將data寫入檔案

@property (readonly)unsigned longlong offsetInFile;

擷取當前檔案的位移量

- (void)seekToFileOffset:(unsignedlong long)offset;

設定當前檔案的位移量

- (unsignedlong long)seekToEndOfFile;

將當前檔案的位移量定位到檔案的末尾

- (void)truncateFileAtOffset:(unsignedlong long)offset;

將檔案的長度設定為offset位元組

- (void)closeFile;

關閉檔案




內容來自於《objective-程式設計第二版》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.