▼iPhoneプログラミングメモ▼
ファイルの読み書き

ファイルの読み書きを行うプログラムを作成する。


iソースコードの記述
FileEx.h
#import <UIKit/UIKit.h>

//FileExの宣言
@interface FileEx : UIViewController <UITextFieldDelegate> {
    UITextField* _textField;
}
@end

FileEx.m
#import "FileEx.h"

#define BTN_WRITE 0
#define BTN_READ  1

//FileExの実装
@implementation FileEx

//テキストフィールドの生成
- (UITextField*)makeTextField:(CGRect)rect text:(NSString*)text {
    UITextField* textField=[[[UITextField alloc] init] autorelease];
    [textField setText:text];
    [textField setFrame:rect];
    [textField setReturnKeyType:UIReturnKeyDone];
    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    [textField setDelegate:self];
    return textField;
}

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:rect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//文字列のバイト配列変換
- (NSData*)str2data:(NSString*)str {
    return [str dataUsingEncoding:NSUTF8StringEncoding];
}

//バイト配列の文字列変換
- (NSString*)data2str:(NSData*)data {
    return [[[NSString alloc] initWithData:data 
        encoding:NSUTF8StringEncoding] autorelease];
}

//バイト配列の書き込み
- (BOOL)data2file:(NSData*)data fileName:(NSString*)fileName {
    NSString* path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path=[path stringByAppendingPathComponent:fileName]; 
    return [data writeToFile:path atomically:YES];
}

//バイト配列の読み込み
- (NSData*)file2data:(NSString*)fileName {
    NSString* path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path=[path stringByAppendingPathComponent:fileName]; 
    return [[[NSData alloc] initWithContentsOfFile:path] autorelease]; 
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];

    //テキストフィールドの生成
    _textField=[[self makeTextField:CGRectMake(0,0,300,32) 
        text:@""] retain];
    [self.view addSubview:_textField];
        
    //書き込みボタンの生成
    UIButton* btnWrite=[self makeButton:CGRectMake(0,50,90,40) 
        text:@"書き込み" tag:BTN_WRITE];
    [self.view addSubview:btnWrite];

    //読み込みボタンの生成
    UIButton* btnRead=[self makeButton:CGRectMake(100,50,90,40) 
        text:@"読み込み" tag:BTN_READ];
    [self.view addSubview:btnRead];
}

//メモリ解放
- (void)dealloc {
    [_textField release];
    [super dealloc];
}

//テキストフィールドのリターン時に呼ばれる
- (BOOL)textFieldShouldReturn:(UITextField*)sender {
    //キーボードを閉じる
    [sender resignFirstResponder];
    return YES;
}

//ボタンクリック時に呼ばれる
- (IBAction)clickButton:(UIButton*)sender {
    if (sender.tag==BTN_WRITE) {
        NSData* data=[self str2data:_textField.text];
        [self data2file:data fileName:@"test.txt"];
    } else if (sender.tag==BTN_READ) {
        NSData* data=[self file2data:@"test.txt"];
        [_textField setText:[self data2str:data]];        
    }
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end

 



−戻る−