简体   繁体   English

在Objective-C中创建制表符分隔文件

[英]Creating tab delimited file in objective-c

I found this snippet online to write, and then append data to a text file: 我发现此代码段可以在线编写,然后将数据附加到文本文件中:

- (void)appendText:(NSString *)text toFile:(NSString *)filePath {

    // NSFileHandle won't create the file for us, so we need to check to make sure it exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {

        // the file doesn't exist yet, so we can just write out the text using the 
        // NSString convenience method

        NSError *error = noErr;
        BOOL success = [text writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
        if (!success) {
            // handle the error
            NSLog(@"%@", error);
        }

    } 
    else {

        // the file already exists, so we should append the text to the end

        // get a handle to the file
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        // move to the end of the file
        [fileHandle seekToEndOfFile];

        // convert the string to an NSData object
        NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding];

        // write the data to the end of the file
        [fileHandle writeData:textData];

        // clean up
        [fileHandle closeFile];
    }
}

This makes sense to me. 这对我来说很有意义。 I have a class that has 3 properties, of NSString, NSInteger, and NSString. 我有一个具有3个属性的类,分别为NSString,NSInteger和NSString。 When I try to use this method, I do this: 当我尝试使用此方法时,我这样做:

for (MyObject *ref in array) {
    NSString *stringToFile = [NSString stringWithFormat:@"%@\t%i\t%@", ref.ChrID, ref.Position, ref.Sequence];
    [self appendText:stringToFile toFile:filePath];
}

It doesn't look quite right. 看起来不太正确。 My data looks like this: 我的数据如下所示:

NSString *tab* NSInteger *single space* NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline 
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
NSStringNSString *tab* NSInteger newline
...

I'm not sure what is going on to make it look like this. 我不确定会怎样使它看起来像这样。 When I NSLog the data, it looks fine. 当我NSLog数据时,它看起来很好。 But something with the first line gets messed up, and then everything seems to be off. 但是第一行的内容弄乱了,然后一切似乎都消失了。 Any thoughts? 有什么想法吗? Thanks. 谢谢。

There are several issues with the method appendText : 方法appendText存在几个问题:

  • if the file does not exist, the first line is written with the NSString writeToFile method without the \\n 如果该文件不存在,则第一行使用NSString writeToFile方法写入,不带\\ n

  • following lines are written with the NSData writeData method 以下几行是使用NSData writeData方法编写的

  • it's very inefficient to use a filemanager to check for existence, get a filehandle, seek to EOF and then write just one line, omitting the close too. 使用文件管理器检查是否存在,获取文件句柄,查找EOF并仅写一行,并且也省略结尾,这是非常低效的。 And repeating this for every following line. 并在接下来的每一行重复此操作。

So better do it this way: 所以最好这样做:

  • get the filehandle for writing, it will be created if it's not there yet 获取要写入的文件句柄,如果尚不存在,则会创建该文件句柄

  • seek to EOF 寻求EOF

  • do your loop with writeData for each line 每行用writeData做循环

  • close the file 关闭档案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM