阅读文本文件作为资源 [英] Read text file as resource
问题描述
我想读从资源文件words.txt。这是我想读逐行一个非常简单的,但是大的(2 MB),文本文件。我已经把文件放到/res/raw/words.txt,并尝试用下面的code打开它:
I am trying to read a file "words.txt" from a resource. It is a very simple, but large (2 MB), text file that I want to read line by line. I have put the file into /res/raw/words.txt, and try to open it with the following code:
try
{
BufferedReader in =
new BufferedReader(
new InputStreamReader(getResources().openRawResource(R.raw.words)));
String line=in.readLine();
T.append(line); T.append("\n");
in.close();
}
catch (Exception e) { T.append(e.toString()); }
不过,我得到一个java.io.IOException异常。这不是例外找不到资源,所以资源被正确打开,但的readLine()产生错误。
However, I get a java.io.IOException. This is not a "resource not found" exception, so the resource is opened correctly, but the readLine() produces the error.
我尝试使用的InputStream本身,与阅读()的结果产生-1,代表EOF,就好像文件是空的。
I tried using the InputStream itself, with the result that read() produces -1, which stands for EOF, as if the file was empty.
我任何帮助吗?
直到现在我还在分手长的文件。因此,这是最好的答案我可以给。任何一个更好的主意吗?
Till now I am still splitting up long files. So this is the best answer I can give. Anyone a better idea?
推荐答案
试试这个:
InputStream is = c.getResources().openRawResource(R.raw.csv_file);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
while ((readLine = br.readLine()) != null) {
}
} catch (IOException e) {
e.printStackTrace();
}
这篇关于阅读文本文件作为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!