用iOS打开一个海峡二进制文件

时间:2013-06-12 01:37:46

标签: ios file-io binaryfiles

我写了一个自定义文件类型,我的Java程序中的数据被保存到。我想在iPod / Pad / Phone上获取数据。到目前为止,我编写了这个文件,用于从文件中读取strait字节值并将其更改为NSStrings和整数。

#import "BinaryFileReader.h"

@implementation BinaryFileReader

- (id)init {
    self = [super init];
    return self;
}

- (id)initWithLocation:(NSString*)filepath {
    if ((self = [super init])) {
        _file = [NSFileHandle fileHandleForReadingAtPath:filepath];
        _fileOffset = 0;
        if (_file == nil)
            NSLog(@"%@%@",@"Failed to open file at path:",filepath);
    }
    return self;
}

- (void)close {
    [_file closeFile];
}

- (int)readInt {
    [_file seekToFileOffset:_fileOffset];
    _databuffer = [_file readDataOfLength:4];
    _fileOffset+=4;
    return (int)[_databuffer bytes];
}

- (NSString*)readNSString {
    int length = [self readInt];
    [_file seekToFileOffset:_fileOffset];
    _databuffer = [_file readDataOfLength:length];
    _fileOffset+=length;
    return [[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding];
}

- (NSMutableArray*)readNSMutableArrayOfNSString {
    NSMutableArray* array = [[NSMutableArray alloc] init];
    int arrayLength = [self readInt];
    int length;
    for (int i=0; i<arrayLength; i++) {
        length = [self readInt];
        [_file seekToFileOffset:_fileOffset];
        _databuffer = [_file readDataOfLength:length];
        _fileOffset+=length;
        [array addObject:[[NSString alloc] initWithData:_databuffer encoding:NSUTF8StringEncoding]];
    }
    return array;
}

@end

现在,当我尝试使用它来读取NSStrings或整数时,它没有提供正确的值。我假设因为NSStrings和整数都出现了错误,所以它在readInt方法中有所作为。任何人都能看到我在这里错过的一些明显的东西吗?

编辑:

我尝试阅读的文件格式以字符串开头。当使用readNSString读取时,该字符串是正确的字符串,但缺少字符串的前1/3。

Java代码:

public void saveItem() {
    try {
        byte[] bytes;
        FileOutputStream output;
        if (countOccurrences(location.getPath(),'.')==1) {
            System.out.println("Option 1");
            output = new FileOutputStream(location+"/"+name+".dtb");
        } else {
            output = new FileOutputStream(location);
        }

        bytes = name.getBytes("UTF-8");
        output.write(bytes.length);
        output.write(bytes);

        output.write(otherNames.length);
        for (int i=0;i<otherNames.length;i++) {
            bytes = otherNames[i].getBytes("UTF-8");
            output.write(bytes.length);
            output.write(bytes);
        }

        bytes = description.getBytes("UTF-8");
        output.write(bytes.length);
        output.write(bytes);

        bytes = XactCode.getBytes("UTF-8");
        output.write(bytes.length);
        output.write(bytes);

        bytes = SymbilityCode.getBytes("UTF-8");
        output.write(bytes.length);
        output.write(bytes);

        output.write(averageLowPrice);

        output.write(averageHighPrice);

        output.write(averageLifeExpectancy);

        output.close();
        bytes = null;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

这不起作用的原因是您将NSData转换为int的方式。 bytes方法将指针返回给实际数据,您需要依赖它来获取实际值(否则您只需获取指针的内存地址)。

我尝试了两种方法:

int test = 64;
NSData *data = [NSData dataWithBytes:&test length:4];
int test2 = (int)[data bytes]; //This gave me 170382992

int test = 64;
NSData *data = [NSData dataWithBytes:&test length:4];
int test2 = *((int *)[data bytes]); //This gave me 64