问题描述
我在我的/ RES /生/文件夹(/res/raw/textfile.txt),而我试图从我处理的android应用程序读取一个资源文件。
公共静态无效的主要(字串[] args){
档案文件=新的文件(RES /生/ TextFile.txt的);
的FileInputStream FIS = NULL;
的BufferedInputStream双= NULL;
DIS的DataInputStream = NULL;
尝试 {
FIS =新的FileInputStream(文件);
双=新的BufferedInputStream(FIS);
DIS =新的DataInputStream(之二);
而(dis.available()!= 0){
//做一些与文件
Log.d(GAME,dis.readLine());
}
fis.close();
bis.close();
透露();
}赶上(FileNotFoundException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
}
我已经尝试了不同的路径语法,但总是得到的 java.io.FileNotFoundException 错误。如何访问/res/raw/textfile.txt进行处理?为文件文件=新的文件(RES /生/ TextFile.txt的); 在Android的错误的方法
的*回答: * 的
//调用LoadText方法,并传递给它RESOURCEID
LoadText(R.raw.textfile);
公共无效LoadText(INT RESOURCEID){
//的InputStream打开RESOURCEID并将其发送至缓冲器
InputStream的是= this.getResources()openRawResource(RESOURCEID)。
的BufferedReader BR =新的BufferedReader(新InputStreamReader的(是));
字符串的readLine = NULL;
尝试 {
//虽然BufferedReader类的readLine不为空
而((的readLine = br.readLine())!= NULL){
Log.d(TEXT,的readLine);
}
//关闭InputStream和BufferedReader
is.close();
br.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
如果你在一个文件中 RES /生/ TextFile.txt的
从活动/控件调用:
getResources()。openRawResource(...)
返回的InputStream
该点实际上应该是在R.raw发现一个整数...对应的文件名,可能是 R.raw.textfile
(通常是该文件的名称不带扩展名)
新的BufferedInputStream(getResources()openRawResource(...));
再读取该文件作为给用户的内容
I have a resource file in my /res/raw/ folder (/res/raw/textfile.txt) which I am trying to read from my android app for processing.
public static void main(String[] args) {
File file = new File("res/raw/textfile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
// Do something with file
Log.d("GAME", dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I have tried different path syntax but always get a java.io.FileNotFoundException error. How can I access /res/raw/textfile.txt for processing? Is File file = new File("res/raw/textfile.txt"); the wrong method in Android?
* Answer: *
// Call the LoadText method and pass it the resourceId
LoadText(R.raw.textfile);
public void LoadText(int resourceId) {
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
If you have a file in res/raw/textfile.txt
from your Activity/Widget call:
getResources().openRawResource(...)
returns an InputStream
The dots should actually be an integer found in R.raw... corresponding to your filename, possibly R.raw.textfile
(it's usually the name of the file without extension)
new BufferedInputStream(getResources().openRawResource(...));
then read the content of the file as a stream
这篇关于在Android的访问资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!