6

我构建一个 Mac 应用程序,我有 2 个问题:

  1. 我想创建一个文本文件来读取和写入数据。我不知道如何创建一个文本文件来读取和写入数据。它使用结构吗?
  2. 我想创建一个 XML 文件来读取和写入数据。我可以为 XML 创建一个结构吗?

你有什么建议吗?提前致谢

4

2 回答 2

31

好吧,要创建一个文件,只需使用

[[NSFileManager defaultManager] createFileAtPath:@"Your/Path" contents:nil attributes:nil];

这将创建一个空文件,您可以对其进行写入或读取。要编写文本(或 XML),只需使用NSString'writeToFile:atomically:encoding:error:这样的方法

NSString *str = //Your text or XML
[str writeToFile:"Your/Path" atomically:YES encoding:NSUTF8StringEncoding error:nil];

要从文件中读取,只需NSString使用该文件的内容

NSString *contents = [NSString stringWithContentsOfFile:@"Your/Path"];

或者,如果它不包含字符串,NSData则从文件中获取一个对象

NSData *contents = [NSData dataWithContentsOfFile:@"Your/Path"];
于 2013-07-22T03:58:26.750 回答
6
/**************************main.m******************************
    NS FILE HANDLE READ & WRITE
    reading and writing in same file
    Created by abdulsathar on 6/16/14.
***************************************************************/

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
   @autoreleasepool  //ARC
    {
        NSFileHandle *file;
        //object for File Handle
        NSError *error;
        //crearing error object for string with file contents format
        NSMutableData *writingdatatofile;
        //create mutable object for ns data
        NSString *filePath=[NSString stringWithFormat:@"/Users/chandrakumar/Documents/abdul/doc.txt"];
        //telling about File Path for Reading for easy of access
        file = [NSFileHandle fileHandleForReadingAtPath:@"/Users/chandrakumar/Documents/abdul/doc.txt"];
        //assign file path directory
            if (file == nil) //check file exist or not
                NSLog(@"Failed to open file");
        NSString *getfileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
        //access file contents with out ns handle method
            if (error) //check error flag for file present or not
                NSLog(@"Error reading file: %@", error.localizedDescription);
        NSLog(@"contents: %@", getfileContents);
        //display file contents in main file
        NSArray *listArray = [getfileContents componentsSeparatedByString:@"\n"];
        //caluculate list of line present in files
        NSLog(@"items = %ld", [listArray count]);
        const char *writingchar = "how are you";
        writingdatatofile = [NSMutableData dataWithBytes:writingchar length:strlen(writingchar)];
        //convert string format into ns mutable data format
        file = [NSFileHandle fileHandleForUpdatingAtPath: @"/Users/chandrakumar/Documents/abdul/new.txt"];
        //set writing path to file
            if (file == nil) //check file present or not in file
                NSLog(@"Failed to open file");
        [file seekToFileOffset: 6];
        //object pointer initialy points the offset as 6 position in file
        [file writeData: writingdatatofile];
        //writing data to new file
        [file closeFile];
        //close the file
    }
    return 0;`enter code here`
}

/***********************************OUTPUT********************************************

 2014-06-17 14:55:39.695 storage[4075:303] contents: hello how are you my dearservice

*************************************************************************************/
于 2014-06-17T09:50:09.953 回答