I was wondering how to read a file from a specific location on ios.
我想知道如何从ios上的特定位置读取文件。
For example I'd quite like to have files of the same name stored in different locations for example.
例如,我非常希望将同名文件存储在不同的位置。
Project/Files/Text/1.txt
项目/ / / 1. txt文本文件
Project/Files/MoreText/1.txt
项目/ / MoreText / 1. txt文件
At the moment I can load files in but I can't see how to specify a specific directory. My program seems to load a file in regardless of where it is meaning I can only have one text file or the other working.
现在我可以加载文件,但是我看不到如何指定一个特定的目录。我的程序似乎加载了一个文件,不管它在哪里意味着我只能有一个文本文件或另一个正在工作。
I have tried using NSFileManager:
我尝试过使用NSFileManager:
NSFileManager *filemanager = [NSFileManager defaultManager];
NSArray *filelist = [filemanager directoryContentsAtPath:@"Text"];
but I later realised that this actually isn't doing anything. It just returns no objects. I've also seen NSBundle but haven't seen an example where a specific file is returned.
但后来我意识到这其实没有任何作用。它只返回无对象。我也见过NSBundle,但没有看到返回特定文件的示例。
I need the filenames to be the same for my algorithm which loads the files.
我需要文件名与加载文件的算法相同。
Any help would be appreciated.
如有任何帮助,我们将不胜感激。
4 个解决方案
#1
11
If you want to read files from the app bundle and you want a certain directory structure, then you need to create a directory in Finder. Then create the directory structure you want in the main directory you just made. Now add the files to the appropriate folders and then add the main directory to your project by dragging it in to Xcode.
如果您想从应用程序包中读取文件,并且您想要一个特定的目录结构,那么您需要在Finder中创建一个目录。然后在刚才创建的主目录中创建所需的目录结构。现在将文件添加到适当的文件夹中,然后将主目录拖放到Xcode中,添加到项目中。
Here is the code to read from the main bundle:
下面是要从主包中读取的代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Text/1" ofType:@"txt"];
NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",testString);
This will read the file "1.txt" from the directory "Text".
这将读取文件“1”。txt"从目录"文本"。
#2
2
iOS application will save all file to root directory, a Group(yellow icon) not a real directory. If you want put your resources into a sub directory, can using this following steps:
iOS应用程序将所有文件保存到根目录,一个组(黄色图标)而不是一个真正的目录。如果你想把你的资源放在一个子目录,可以使用以下步骤:
- Create a directory in the Finder, put your files into this directory.
- 在查找程序中创建一个目录,将文件放到这个目录中。
- In Xcode, select "Add Files to..." from File menu.
- 在Xcode中,从File菜单中选择“向…添加文件”。
- Select "Copy items into destination group's folder" and "Create Folder References for any added folders" in the popup.
- 在弹出窗口中选择“将项目复制到目标组的文件夹”和“为任何添加的文件夹创建文件夹引用”。
Then, you will found a blue icon directory in your project, It is a real directory.
然后,您将在项目中找到一个蓝色的图标目录,它是一个真正的目录。
Now, you can read the files use NSBundle Class method:
现在,您可以阅读文件使用NSBundle类方法:
+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath
#3
0
first of all, keep in mind that by default all files goes to 1 directory after compiling. To put them to directory you have to specify that in xCode. As for loading goes you can just enter relative path "MyDir/myFile.txt"
首先,请记住,在编译之后,默认情况下所有文件都将进入1个目录。要将它们放到目录中,必须在xCode中指定。至于加载,你可以输入相对路径"MyDir/myFile.txt"
#4
0
Relative paths don't work on Mac OS X--this is because the current directory is ignored by Cocoa, and changes pretty nearly at random. You need to always work with an absolute path. To get the absolute path to your app's Resources
directory, use:
相对路径在Mac OS X上不起作用——这是因为当前目录被Cocoa忽略,并且几乎是随机变化的。您需要始终使用绝对路径。要获得应用程序资源目录的绝对路径,请使用:
NSString *resDir = [[NSBundle mainBundle] resourcePath];
Then to enumerate the contents of the Text
directory in that directory:
然后列举该目录下的文本目录的内容:
NSString *textDir = [resDir stringByAppendingPathComponent: @"Text"];
NSArray *textFiles = [[NSFileManager defaultManager] directoryContentsAtPath: textDir];
However, Xcode may or may not honour the file hierarchy in your app's Resources
directory. To determine if files are where you expect them to be, you should build your app, right-click the .app
bundle, and select Show Package Contents
.
但是,Xcode可能会也可能不会遵守应用程序资源目录中的文件层次结构。要确定文件是否在预期的位置,应该构建应用程序,右键单击.app bundle,选择Show Package Contents。
#1
11
If you want to read files from the app bundle and you want a certain directory structure, then you need to create a directory in Finder. Then create the directory structure you want in the main directory you just made. Now add the files to the appropriate folders and then add the main directory to your project by dragging it in to Xcode.
如果您想从应用程序包中读取文件,并且您想要一个特定的目录结构,那么您需要在Finder中创建一个目录。然后在刚才创建的主目录中创建所需的目录结构。现在将文件添加到适当的文件夹中,然后将主目录拖放到Xcode中,添加到项目中。
Here is the code to read from the main bundle:
下面是要从主包中读取的代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Text/1" ofType:@"txt"];
NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",testString);
This will read the file "1.txt" from the directory "Text".
这将读取文件“1”。txt"从目录"文本"。
#2
2
iOS application will save all file to root directory, a Group(yellow icon) not a real directory. If you want put your resources into a sub directory, can using this following steps:
iOS应用程序将所有文件保存到根目录,一个组(黄色图标)而不是一个真正的目录。如果你想把你的资源放在一个子目录,可以使用以下步骤:
- Create a directory in the Finder, put your files into this directory.
- 在查找程序中创建一个目录,将文件放到这个目录中。
- In Xcode, select "Add Files to..." from File menu.
- 在Xcode中,从File菜单中选择“向…添加文件”。
- Select "Copy items into destination group's folder" and "Create Folder References for any added folders" in the popup.
- 在弹出窗口中选择“将项目复制到目标组的文件夹”和“为任何添加的文件夹创建文件夹引用”。
Then, you will found a blue icon directory in your project, It is a real directory.
然后,您将在项目中找到一个蓝色的图标目录,它是一个真正的目录。
Now, you can read the files use NSBundle Class method:
现在,您可以阅读文件使用NSBundle类方法:
+ (NSArray *)pathsForResourcesOfType:(NSString *)extension inDirectory:(NSString *)bundlePath
#3
0
first of all, keep in mind that by default all files goes to 1 directory after compiling. To put them to directory you have to specify that in xCode. As for loading goes you can just enter relative path "MyDir/myFile.txt"
首先,请记住,在编译之后,默认情况下所有文件都将进入1个目录。要将它们放到目录中,必须在xCode中指定。至于加载,你可以输入相对路径"MyDir/myFile.txt"
#4
0
Relative paths don't work on Mac OS X--this is because the current directory is ignored by Cocoa, and changes pretty nearly at random. You need to always work with an absolute path. To get the absolute path to your app's Resources
directory, use:
相对路径在Mac OS X上不起作用——这是因为当前目录被Cocoa忽略,并且几乎是随机变化的。您需要始终使用绝对路径。要获得应用程序资源目录的绝对路径,请使用:
NSString *resDir = [[NSBundle mainBundle] resourcePath];
Then to enumerate the contents of the Text
directory in that directory:
然后列举该目录下的文本目录的内容:
NSString *textDir = [resDir stringByAppendingPathComponent: @"Text"];
NSArray *textFiles = [[NSFileManager defaultManager] directoryContentsAtPath: textDir];
However, Xcode may or may not honour the file hierarchy in your app's Resources
directory. To determine if files are where you expect them to be, you should build your app, right-click the .app
bundle, and select Show Package Contents
.
但是,Xcode可能会也可能不会遵守应用程序资源目录中的文件层次结构。要确定文件是否在预期的位置,应该构建应用程序,右键单击.app bundle,选择Show Package Contents。