2010-12-16 28 views
-1

我是Objective-C的新手。現在,我正在處理文件。我的問題是.. 如何搜索特定文件夾中的文件?如何使用Objective-C讀取特定文件夾中的文件?

比方說,我得到一個輸入字符串「輸入」。現在我想要做的就是搜索並從特定路徑「user/desktop/files」文件夾中讀取文件「input.txt」。我該怎麼做呢? 我知道使用NSFileHandle通過指定完整路徑來讀取文件。但我不知道如何做到這一點。 請幫我一把。

感謝

回答

3

有一些NSString方法你應該考慮使用。

// path contains the path to the folder you want to find the file in. 

// create the full file name 

NSString* shortPath = [@"input" stringByAppendingPathExtension: @"txt"]; 
NSString* fullPath = [path stringByAppendingPathComponent: shortFileName]; 

// open the file. we convert to an NSURL so we can use the better open method 
NSURL* fileURL = [NSURL fileURLWithPath: fullPath]; 
NSError* error = nil; 
NSFileHandle *file=[NSFileHandle fileHandleForReadingFromURL: fileURL 
                 error: &error]; 
if (file == nil) 
{ 
    // error contains info on what went wrong 
} 
else 
{ 
    // do what you need 
} 
+0

我認爲你想'輸入'純粹沒有@「」,並且還有一個額外的P字符串** P ** ByAppendingPathComponent :(可能會遲到)。我討厭不得不輸入一個網頁瀏覽器... – NSGod 2010-12-16 09:10:52

+0

不要介意我的第一個評論,關於想要沒有@「」的簡單輸入。它正在遲到,哈哈。 – NSGod 2010-12-16 09:24:23

+0

@NSGod:這裏早... – JeremyP 2010-12-16 09:27:26

0

好吧,我只是做了與NSString的東西在這裏...

NSString *input=[TextInput stringValue]; 
NSString *[email protected]"/Users/mith/Desktop/files/"; 
NSString *fullpath=[NSString stringWithFormat:@"%@%@.txt",path,input]; 
NSFileHandle *file=[NSFileHandle fileHandleForReadingAtPath:fullpath];` 

現在好了,這個工作對我來說很好...但是,有沒有更好的方式來做到這一點?

謝謝

3

一般來說,路徑,您使用的是在NSPathUtilities.h定義(而不是在NSString.h本身)以下方法來工作:

@interface NSString (NSStringPathExtensions) 

+ (NSString *)pathWithComponents:(NSArray *)components; 
- (NSArray *)pathComponents; 

- (BOOL)isAbsolutePath; 

- (NSString *)lastPathComponent; // frequently-used 
- (NSString *)stringByDeletingLastPathComponent; // frequently-used 
- (NSString *)stringByAppendingPathComponent:(NSString *)str; // frequently-used 

- (NSString *)pathExtension; // frequently-used 
- (NSString *)stringByDeletingPathExtension; // frequently-used 
- (NSString *)stringByAppendingPathExtension:(NSString *)str; // frequently-used 

- (NSString *)stringByAbbreviatingWithTildeInPath; 
- (NSString *)stringByExpandingTildeInPath; 

- (NSString *)stringByStandardizingPath; 

- (NSString *)stringByResolvingSymlinksInPath; 

- (NSArray *)stringsByAppendingPaths:(NSArray *)paths; 

- (NSUInteger)completePathIntoString:(NSString **)outputName 
    caseSensitive:(BOOL)flag matchesIntoArray:(NSArray **)outputArray 
    filterTypes:(NSArray *)filterTypes; 

- (__strong const char *)fileSystemRepresentation; 
- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max; 

@end 

所以,你會做什麼如:

NSString *fullpath = [[path stringByAppendingPathComponent:input] 
    stringByAppendingPathExtension:@"txt"]; 

這些方法負責爲您處理路徑分隔符。

如果文件被稱爲是一個文本文件,可以使用以下方法中的NSString:

- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc 
    error:(NSError **)error; 
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc 
    error:(NSError **)error; 
+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc 
    error:(NSError **)error; 
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc 
    error:(NSError **)error; 

/* These try to determine the encoding, and return the encoding which was used. 
     Note that these methods might get "smarter" in subsequent releases of the 
    system, and use additional techniques for recognizing encodings. If nil 
    is returned, the optional error return indicates problem that was 
    encountered (for instance, file system or encoding errors). */ 
- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc 
    error:(NSError **)error; 
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc 
    error:(NSError **)error; 
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc 
    error:(NSError **)error; 
+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc 
    error:(NSError **)error; 

/* Write to specified url or path using the specified encoding. 
    The optional error return is to indicate file system or encoding errors. 
*/ 
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile 
    encoding:(NSStringEncoding)enc error:(NSError **)error; 
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile 
    encoding:(NSStringEncoding)enc error:(NSError **)error; 

後者可以用來寫入文件。

你會發現Cocoa中的許多類都有方法從文件或URL本身讀入。例如,NSImage有一個從文件讀取的方法。您通常可以考慮高於文件句柄的項目(儘管它們確實有其自己的位置)。對於通用數據,總是有NSData,它們也可以從文件讀入。

要以編程方式獲取文件夾中項目的列表,可以使用NSFileManager,然後使用NSString的方法構建單個項目的路徑。

哦,另一個提示,如果你不知道。如果您在Xcode源代碼窗口中,請按住Command鍵並雙擊任何類或方法名稱或數據類型以打開該對象的頭文件。例如,命令 - 雙擊NSString打開NSString.h。按住Command-Option並雙擊以打開Xcode幫助窗口並搜索突出顯示的術語。

相關問題