你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 文件讀寫操作詳解及簡單實例

IOS 文件讀寫操作詳解及簡單實例

編輯:IOS開發綜合

iPhone 文件讀寫操作

1,寫文件操作

- (IBAction)btnWrite:(id)sender {
  //創建文件管理器
  NSFileManager *fileManager = [NSFileManager defaultManager];
  //獲取路徑
  //參數 要獲取的哪種路徑
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路徑
  //更改到操作的目錄下
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   //創建文件fileName文件名稱,contents文件內容,如果開始沒有內容可以設置為nil,attributes文件的屬性,初始為nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  //刪除待刪除文件
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  //獲取文件路徑
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  //待寫入的數據
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  //創建數據緩沖
  NSMutableData *writer = [[NSMutableData alloc]init];
  //將字符串添加到緩沖中
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  [writer appendBytes:&data0 length:sizeof(data0)];
  [writer appendBytes:&data2 length:sizeof(data2)];
   
  [writer writeToFile:path atomically:YES];
  [writer release];  
}

2,讀文件操作

- (IBAction)btnRead:(id)sender {
  //創建文件管理器
  NSFileManager *fileManager = [NSFileManager defaultManager];
  //獲取路徑
  //參數 要獲取的哪種路徑
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路徑
  //更改到操作的目錄下
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   //創建文件fileName文件名稱,contents文件內容,如果開始沒有內容可以設置為nil,attributes文件的屬性,初始為nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  //刪除待刪除文件
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  //獲取文件路徑
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  //待寫入的數據
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  //創建數據緩沖
  NSMutableData *writer = [[NSMutableData alloc]init];
  //將字符串添加到緩沖中
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  [writer appendBytes:&data0 length:sizeof(data0)];
  [writer appendBytes:&data2 length:sizeof(data2)];
   
  [writer writeToFile:path atomically:YES];
  [writer release];
  
  //讀取操作
  int gData0;
  float gData1;
  NSString *gData2;
  
  NSData *reader = [NSData dataWithContentsOfFile:path];
  gData2 = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding];
  [reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))];
  [reader getBytes:&gData1 range:NSMakeRange([temp length]+ sizeof(gData0) , sizeof(gData1))];
  NSLog(@"gData0 == %d",gData0);
  NSLog(@"gData1 == %f",gData1);
  NSLog(@"gData2 == %@",gData2);
  
  // lblText.text = gData2;
   
}

 iphone實現文件的讀寫操作

iphone可以很容易的實現文件的讀寫,但是如果你沒有使用蘋果公司99美元開發者sdk的話,你利用破解打出來的api包,安裝到手機上不能把文件寫入手機.下面是我實現讀寫的代碼:

#import "ManagerFile.h"

@implementation ManagerFile

-(void)writeFile:(NSString *)file 
{ 
  //創建文件管理器 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  //獲取路徑 
  //參數NSDocumentDirectory要獲取那種路徑 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];//去處需要的路徑  
  //更改到待操作的目錄下 
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  //創建文件fileName文件名稱,contents文件的內容,如果開始沒有內容可以設置為nil,attributes文件的屬性,初始為nil 
  //獲取文件路徑 
  [fileManager removeItemAtPath:@"username" error:nil]; 
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  //創建數據緩沖 
  NSMutableData *writer = [[NSMutableData alloc] init]; 
  //將字符串添加到緩沖中 
  [writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]]; 
  //將其他數據添加到緩沖中 
  //將緩沖的數據寫入到文件中 
  [writer writeToFile:path atomically:YES]; 
  [writer release]; 
} 
-(NSString *)readFile 
{ 
  //創建文件管理器 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  //獲取路徑 
  //參數NSDocumentDirectory要獲取那種路徑 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];//去處需要的路徑  
  //更改到待操作的目錄下 
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  //獲取文件路徑 
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  NSData *reader = [NSData dataWithContentsOfFile:path]; 
  return [[NSString alloc] initWithData:reader 
                 encoding:NSUTF8StringEncoding]; 
} 
@end

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved