从文件中读取内容

发布于 2024-09-12 06:45:54 字数 1122 浏览 3 评论 0原文

NSString *contentPath = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"rtf"];
NSData *databuffer;

NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:contentPath];

if (file == nil)
    NSLog(@"Failed to open file");

databuffer = [file readDataToEndOfFile];

[file closeFile];

NSString *contentText =[[NSString alloc] initWithData:databuffer encoding:NSUTF8StringEncoding];

debugLog(@"%@",contentText);

[textView setText:contentText];
[contentText release];

//[textView setText:[bookObj commentary]];
[self.view addSubview:textView];

输出:

i\ansicpg1252\cocoartf1038\cocoasubrtf320
{\fonttbl\f0\froman\fcharset0 Times-Roman;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\ql\qnatural

\f0\fs24 \cf0 流是编程中的基本抽象:从一个点串行传输到另一个点的一系列位。 Cocoa 提供了三个类来表示流并方便它们在程序中的使用:NSStream、NSInputStream 和 NSOutputStream。通过这些类的实例,您可以从文件和应用程序内存中读取数据,并向其中写入数据。您还可以在基于套接字的情况下使用这些对象

如何过滤那些粗体数据..???

NSString *contentPath = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"rtf"];
NSData *databuffer;

NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:contentPath];

if (file == nil)
    NSLog(@"Failed to open file");

databuffer = [file readDataToEndOfFile];

[file closeFile];

NSString *contentText =[[NSString alloc] initWithData:databuffer encoding:NSUTF8StringEncoding];

debugLog(@"%@",contentText);

[textView setText:contentText];
[contentText release];

//[textView setText:[bookObj commentary]];
[self.view addSubview:textView];

Output:

i\ansicpg1252\cocoartf1038\cocoasubrtf320
{\fonttbl\f0\froman\fcharset0 Times-Roman;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\ql\qnatural

\f0\fs24 \cf0 A stream is a fundamental abstraction in programming: a sequence of bits transmitted serially from one point to another point. Cocoa provides three classes to represent streams and facilitate their use in your programs: NSStream, NSInputStream, and NSOutputStream. With the instances of these classes you can read data from, and write data to, files and application memory. You can also use these objects in socket-based

How to filter those bold data..???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-09-19 06:45:54

您正在尝试将 RTF 文本加载到纯字符串中。请改用 NSAttributedString 或将文件转换为纯文本。

You are trying to load RTF text into a plain string. Use NSAttributedString instead or convert your file to plain text.

生生漫 2024-09-19 06:45:54

我不知道您是否也打算编辑此文本或只是显示它,但我知道的唯一可行的方法是使用 UIWebView

NSString *path = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"rtf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

I dont know if you were planning to edit this text as well or just display it but the only workable way I know is to use a UIWebView

NSString *path = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"rtf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文