간단한 파일 입출력 Object C

펌 : http://www.ddandongne.com/ddanhome/zbuilder/2208


Document폴더에는 읽기 쓰기 모두 가능.(펌  : http://cafe.naver.com/mcbugi/16650)

#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:@"data.txt"];

/로 경로 표시


1. 특징
    - 사용자 클래스는 저장할 수 없다.

    - 저장할 수 있는 객체는 기본 컬랙션 객체와 기본 객체들

        NSArray,NSMutableArray, NSDictionary, NSMutableDictioinary, NSData, NSString, NSNumber . . .


2. 파일이 저장되는 위치는?

   - documents 

  

01.// 데이터 파일 경로 구하기.
02.- (NSString *)dataFilePath
03.{
04.// 읽고 쓰기 간으한 document디렉토리를 구한다.
05.NSArray *paths = NSSearchPathForDirectoriesInDomains(
06.NSDocumentDirectory, NSUserDomainMask, YES);
07.// 아이폰에서는 NSSearchPathForDirectoriesInDomains로 구한 정보가
08.// 도큐먼트 디렉토리 오직 하나임.
09.NSString *documentsDirectory = [paths objectAtIndex:0];
10. 
11.return [documentsDirectory stringByAppendingPathComponent:kFileName];
12.}


3. 저장.

1.NSMutableArray        *array        =[[NSMutableArray alloc] init];
2.[array addObject:@"data1"];
3.[array addObject:@"data2"]; 
4.[array writeToFile:[self dataFilePath ] atomically:YES];
5.[array release];


4. 읽기.

1.NSArray *array            =[[NSArray alloc] initWithContentsOfFile:filePath];
2.field1.text                  =[array objectAtIndex:0];
3.field2.text                  =[array objectAtIndex:1];
4.[array release];

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://upthere9.egloos.com/tb/2835657 [도움말]

덧글

댓글 입력 영역