IOS NSFileHandle的使用七 (NSFileHandle的概念和用法)

1、讀取文件類和經常使用方法
spa

    NSFileManager類主要對文件的操做(刪除、修改、移動、複製等等)。
code

    NSFileHandle類主要對文件內容進行讀取和寫入操做,能夠使用NSFileHandle作文件的斷點續傳。
對象

    NSFileHandle處理文件的步驟:
get

    一、建立一個NSFileHandle對象;
string

    二、對打開的文件進行I/O操做;
it

    三、關閉文件;
class

2、經常使用方法 test

+ (id)fileHandleForReadingAtPath:(NSString *)path;    //打開一個文件只准讀
+ (id)fileHandleForWritingAtPath:(NSString *)path;    //打開一個文件只准寫
+ (id)fileHandleForUpdatingAtPath:(NSString *)path;    //打開一個文件可讀寫
//
- (NSData *)availableData;    //從設備或通道返回可用的數據
- (NSData *)readDataToEndOfFile;    //從當前位置讀取到文件尾
- (NSData *)readDataOfLength:(NSUInteger)length;    //讀取指定長度的內容
//
- (void)writeData:(NSData *)data;    //寫數據

- (unsigned long long)offsetInFile;    //獲取當前文件偏移量
- (unsigned long long)seekToEndOfFile;    //移動到文件尾
- (void)seekToFileOffset:(unsigned long long)offset;    //移動到指定偏移量
- (void)truncateFileAtOffset:(unsigned long long)offset;    //設置文件爲指定大小
- (void)closeFile;    //關閉文件

3、相關代碼以下:coding

    一、向指定文件中寫入數據
file

        NSString *homepath = NSHomeDirectory();
        NSLog(@"homepath:%@",homepath);
        NSString *filepath = [homepath stringByAppendingPathComponent:@"Data/TestData/test.txt"];
        NSLog(@"filepath:%@",filepath);
        
        NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:filepath];
        //NSFileHandle *filehandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
        
        [filehandle seekToEndOfFile];
        NSString *str = @"追加的數據";
        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        [filehandle writeData:data];
        [filehandle closeFile];

    二、向指定文件中讀出數據

        NSString *homepath = NSHomeDirectory();
        NSString *filepath = [homepath stringByAppendingPathComponent:@"Data/TestData/test.txt"];
        NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:filepath];
        
        NSUInteger length = [filehandle availableData].length;
        [filehandle seekToFileOffset:length/2];
        
        NSData *data = [filehandle readDataToEndOfFile];
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"the data is :%@",str);
        [filehandle closeFile];

    三、對文件進行相關操做

        NSString *homepath = NSHomeDirectory();
        NSString *filepath = [homepath stringByAppendingPathComponent:@"Data/TestData/test.txt"];
        NSString *targetpath = [homepath stringByAppendingPathComponent:@"Data/TestData/test2.txt"];
        
        NSFileManager *filemanager = [NSFileManager defaultManager];
        
        BOOL result = [filemanager createFileAtPath:targetpath contents:nil attributes:nil];
        if (result == true) {
            NSLog(@"文件建立成功!");
        }
        
        NSFileHandle *sourcefilehandle = [NSFileHandle fileHandleForReadingAtPath:filepath];
        NSFileHandle *targetfilehandele = [NSFileHandle fileHandleForWritingAtPath:targetpath];
        
        NSData *data = [sourcefilehandle readDataToEndOfFile];
        [targetfilehandele writeData:data];
        
        [sourcefilehandle closeFile];
        [targetfilehandele closeFile];
相關文章
相關標籤/搜索